iphone SDK:如何设置UIPickerView的初始值?

jp *_*nce 18 iphone

我有一个选择器视图,其中包含可供选择的数字列表.我希望能够在特定行初始化控件.例如,如果用户将10指定为其默认值,则在首次初始化控件时,控件应自动定位在该行.

提前致谢.

Tec*_*Zen 55

您调用selectRow:inComponent:animated:并传递要选择的行的索引.

此代码导致a UIPickerView在0到60之间旋转其数字0到60.

- (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];
}
Run Code Online (Sandbox Code Playgroud)

在您的情况下,要显示第十行,您可以调用以下内容:

[thePicker selectRow:10 inComponent:0 animated:YES];
Run Code Online (Sandbox Code Playgroud)