我有一个选择器视图,其中包含可供选择的数字列表。我希望能够在特定行初始化控件。例如,如果用户将10指定为其默认值,则在首次初始化控件时,控件应自动定位在该行。
提前致谢。
答案 0 :(得分:55)
您调用selectRow:inComponent:animated:
并将其传递给您想要选择的行的索引。
此代码会导致UIPickerView
在0到60之间旋转,然后停在0上。
- (void)viewDidAppear:(BOOL)animated {
[thePicker selectRow:60 inComponent:0 animated:YES];
[thePicker reloadComponent:0];
[thePicker selectRow:60 inComponent:1 animated:YES];
[thePicker reloadComponent:1];
[thePicker selectRow:0 inComponent:0 animated:YES];
[thePicker reloadComponent:0];
[thePicker selectRow:0 inComponent:1 animated:YES];
[thePicker reloadComponent:1];
}
在您的情况下,要显示第十行,您可以调用类似:
[thePicker selectRow:10 inComponent:0 animated:YES];
答案 1 :(得分:0)
你可以做的不是选择行,而是让pickerview找到你想要的值。方法是创建一个循环以在数组中找到它并选择它:
- (void)viewDidAppear:(BOOL)animated{
int i = 0;
for(NSString* number in arrayName)
{
if ([stringValue isEqualToString:number]){
[pickerViewName selectRow:i inComponent:0 animated:YES ];
}
i++;
}
}
答案 2 :(得分:0)
如果您将UITextField
与UIPickerView
[self.text_field setInputView:self.picker];
然后使用它:
self.text_field.text = [_myarray objectAtIndex:10];
答案 3 :(得分:0)
尝试一下,它对我来说很有效,只需一行代码。
在Objective-C中
[self.picker selectRow:2 inComponent:0 animated:YES];
在Swift中
picker.selectRow(2, inComponent:0, animated:true)
希望这会有所帮助。