我有一个pickerview,它从视图控制器上的核心数据提取加载了load方法。一切都很好,但我发现我经常传递空值,而且我必须来回旋转选择器并点击几次以获得值。
如果用户没有手动选择某些东西我也有问题,我需要它来传递选择器所在的值。我在同一个viewcontroller上有一个datepicker,它会传递它所处的任何日期而没有任何问题。我可能会忽略一些东西......非常感谢
# 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;
}
}
答案 0 :(得分:1)
您需要将selectedProfile
初始化为选择器中的第一个值。在viewDidLoad
中执行此操作。这样,如果用户从未明确选择值,则默认情况下selectedProfile
将设置为第1个值。