简体   繁体   English

如何将选择器视图的指示器设置为特定值

[英]how to set indicator of picker view to specific value

My cell is containing a button when you click a button, the picker view will pop op just like an image below 当您单击按钮时,我的单元格包含一个按钮,选择器视图将像下面的图像一样弹出操作 在此输入图像描述

Question : 题 :

How can I change so that the indicator will move to the correct value which is shown in my button. 如何更改以使指示器移动到我的按钮中显示的正确值。

For example, my button is showing 8 and if you tap on it, the picker view will be popped up with indicator choosing 8. 例如,我的按钮显示为8,如果点击它,将弹出选择器视图,指示符选择8。

If you know how to customize this. 如果你知道如何自定义它。 please advice me on this issue. 请就此问题向我提出建议。 Thanks 谢谢

You can implement the following: 您可以实现以下内容:

- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated

You can use it by adding an IBAction for the button. 您可以通过为按钮添加IBAction来使用它。

-(IBAction)userTappedNumber{
    //Call your picker view's method here.
    [self.pickerView selectRow:8 inComponent:0 animated:YES];
}

For future reference try taking a look at Apple's references and searching for similar questions via google and the Stack Overflow search before asking simple questions. 为了将来参考,请先查看Apple的参考资料,然后在询问简单问题之前通过Google和Stack Overflow搜索搜索类似的问题。 Apple's UIPickerView Class Reference Apple的UIPickerView类参考

使用这种方法

- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated

There is a way to find a specific value without knowing its position in the array. 有一种方法可以在不知道其在数组中的位置的情况下找到特定值。

First, convert the value that you want to look for to string 首先,将要查找的值转换为字符串

stringValue = [NSString stringWithFormat:@"%d",intValue];

Second, create a loop to find the value in the array and select it: 其次,创建一个循环来查找数组中的值并选择它:

int i = 0;
    for(NSString* number in arrayName)
    {
        if ([stringValue isEqualToString:number]){
            [pickerViewName selectRow:i inComponent:0 animated:YES ];
        }
        i++;
    }

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

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