繁体   English   中英

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

[英]iphone SDK: How to set the initial value of a UIPickerView?

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

提前致谢。

您调用selectRow:inComponent:animated:并将其传递给您想要选择的行的索引。

此代码导致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];
}

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

[thePicker selectRow:10 inComponent:0 animated:YES];

你可以做的,而不是选择行,是让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++;
    }
}

如果您正在使用UITextFieldUIPickerView

[self.text_field setInputView:self.picker];

然后用这个:

self.text_field.text = [_myarray objectAtIndex:10];

试试这个它对我有用,只需一行代码。

在Objective-C中

   [self.picker selectRow:2 inComponent:0 animated:YES];

在斯威夫特

  picker.selectRow(2, inComponent:0, animated:true)

希望这对一些人有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM