我在popover中有一个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];
}
每次单击此按钮时,我都不关心创建popover和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)];
}
我的问题是,每次去展示popover时,我都会在[timePicker selectRow...
行上看到NSRangeException。这种方式对我来说很有意义,因为UIPickerView尚未显示。但是使用那个逻辑......如何为UIPickerView设置默认/起始值?
我确信这里有一个简单的解决方案,但我只是没有看到它......
由于
答案 0 :(得分:2)
检查组件数量是否有效且currentStep.length - 1
是否不大于8
。
您的主要问题是您没有设置选择器的dataSource
。添加
timePicker.dataSource = self;