如果没有触及PickerView,UIPickerView会选择第零行

时间:2013-04-04 15:43:19

标签: xcode uipickerview

我试图消除PickerView的一个烦人的事情是,如果用户必须从零行移开并再次重新选择它。我希望如果用户决定选择零行,他只需按下按钮即可转到下一个视图,并选择行零值。

任何想法?

非常感谢

这是我的代码 - 目前,如果按下连接到saveSelectedCondition的按钮,并且pickerView没有从第0行移开,则为selectedCondition返回null,而不是第0行的条件值:

   - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

        Condition *condition = [[self.fetchedResultsController fetchedObjects] objectAtIndex:row];
        self.selectedCondition = condition;
        NSLog(@"The '%@' condition was selected using the picker", self.selectedCondition.name);
    }

    - (IBAction)saveSelectedCondition:(id)sender {
        [self.selectedCondition setValue:@"YES" forKey:@"isSelectedCondition"];
        [self.managedObjectContext save:nil];
        NSLog(@"Saving selected condition (%@) with isSelectedCondition set to '%@'", self.selectedCondition.name, self.selectedCondition.isSelectedCondition);
    }

2 个答案:

答案 0 :(得分:0)

试试吧......

- (IBAction)saveSelectedCondition:(id)sender {
    myText.text = [myArray objectAtIndex:0];//Just add this line for set first value
    [self.selectedCondition setValue:@"YES" forKey:@"isSelectedCondition"];
    [self.managedObjectContext save:nil];
    NSLog(@"Saving selected condition (%@) with isSelectedCondition set to '%@'", self.selectedCondition.name, self.selectedCondition.isSelectedCondition);
}

Set first value when your picker view called.

答案 1 :(得分:0)

我现在使用以下代码在performFetch之后将其整理出来:

 [self performFetch];
    NSLog(@"The following Conditions were fetched for the Picker by SelectorViewController:");
    for (Condition *fetchedCondition in [self.fetchedResultsController fetchedObjects]) {
        if (fetchedCondition ==[[self.fetchedResultsController fetchedObjects] objectAtIndex:0])
        {[fetchedCondition setValue:@"YES" forKey:@"isSelectedCondition"];
            self.selectedCondition = fetchedCondition;}
        else
        [fetchedCondition setValue:@"NO" forKey:@"isSelectedCondition"];    
        [self.managedObjectContext save:nil];
        NSLog(@"Condition: %@, isSelectedCondition %@", fetchedCondition.name, fetchedCondition.isSelectedCondition);
    }

}