2013-10-31 74 views
0

我有一個pickerview,從視圖控制器上的核心數據讀取加載方法。一切都很好,但是我發現我常常傳遞null值,並且我不得不來回旋轉選擇器並點擊幾次以獲取值。從UIPickerView獲取沒有選擇的行

我也有一個問題,如果用戶不手動選擇的東西,我需要它通過選擇器的價值。我在同一個viewcontroller上有一個日期選擇器,並且它傳遞了它正在坐的任何日期而沒有任何問題。我可能俯瞰something..any幫助將不勝感激

# pragma mark PickerView Section 

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
    return 1; // returns the number of columns to display. 
} 

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    return [profiles count]; // returns the number of rows 
} 

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    // Display the profiles we've fetched on the picker 

    Profiles *prof = [profiles objectAtIndex:row]; 
    return prof.profilename; 
} 

//If the user chooses from the pickerview 

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
    selectedProfile = [[profiles objectAtIndex:row]valueForKey:@"profilename"]; 

} 


# pragma mark Segue Section 

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 

    if ([[segue identifier] isEqualToString:@"resultsSegue"]) { 

     ResultsVC *result = segue.destinationViewController; 

     result.profileName = [NSString stringWithFormat:@"%@",selectedProfile]; 

     NSDate *selectedDate = [datePicker date]; 
     result.trialDate = selectedDate; 

    } 
} 

回答

1

需要初始化selectedProfile在選擇器的第一個值。在viewDidLoad中這樣做。這樣,如果用戶從未明確地選擇一個值,默認情況下selectedProfile將被設置爲第一個值。

+0

完美!我覺得自己像個白癡! – Mark