我在弹出窗口中有一个 UIPickerView 。我只是想在显示弹出框之前设置选择器视图的默认/起始值。这是我用来创建和显示弹出框的代码(在处理触摸按钮操作的方法中):
- (IBAction) handleClickStepTimeButton: (id)sender
{
UIViewController *timePickerController = [UIViewController alloc];
UIPickerView *timePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 100, 180)];
timePicker.delegate = self;
timePicker.showsSelectionIndicator = YES;
[timePickerController.view addSubview:timePicker];
UIPopoverController *timePickerPopoverController = [[UIPopoverController alloc] initWithContentViewController:timePickerController];
timePickerPopoverController.popoverContentSize = CGSizeMake(100, 200);
UIButton *stepTimeButton = (UIButton *)sender;
[timePickerPopoverController presentPopoverFromRect:CGRectMake(stepTimeButton.frame.size.width, (stepTimeButton.frame.size.height / 2), 1, 1) inView:stepTimeButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[timePicker selectRow:(currentStep.length - 1) inComponent:1 animated:YES];
}
每次单击此按钮时,我都不担心创建弹出框和 UIPickerView,因为它不会经常发生,并且 UIPickerView 的值只是少数整数。这是我用来将值添加到视图的代码:
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return 8;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)rowId forComponent:(NSInteger)component {
return [NSString stringWithFormat:@"%d", (rowId + 1)];
}
我的问题是,每次我去显示弹出窗口时,我都会得到一个 NSRangeException 就[timePicker selectRow...
行了。这种对我来说很有意义,因为 UIPickerView 还没有显示。但是使用该逻辑......如何为 UIPickerView 设置默认/起始值?
我确定这里有一个简单的解决方案,但我只是没有看到它......
谢谢