iOS UIPickerView 如何在委托方法之外获取选中的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18596109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:58:00  来源:igfitidea点击:

iOS UIPickerView how to get the selected row outside the delegate method

iosobjective-ccocoa-touch

提问by Bogdan Alexandru

I know the UIPickerView has this method:

我知道 UIPickerView 有这个方法:

- (void)pickerView:(UIPickerView *)pickerView
      didSelectRow:(NSInteger)row
       inComponent:(NSInteger)component

But inside this method, I want to get the selected row of other components. How on Earth can I do that?

但是在此方法中,我想获取其他组件的选定行。我到底怎么能做到这一点?

回答by iPatel

You can get selected row number anywhere by using selectedRowInComponentmethod of UIPickerView.

您可以使用 的selectedRowInComponent方法在任何地方获取选定的行号UIPickerView

Such like

像这样

NSString *YourselectedTitle = [self.yourArrayName objectAtIndex:[self.yourPickerName selectedRowInComponent:0]];
NSLog(@"%@", YourselectedTitle);

回答by Dani

Old question but if someone like me is working with Swift 2.0 now the answer is

老问题,但如果像我这样的人现在正在使用 Swift 2.0,答案是

let row = self.myPicker.selectedRowInComponent(0)
let value = myArray[row]

I wrote 0 to component value because my picker has only one component, but it could have more than one such as datepicker with Date and Time

我将 0 写入组件值,因为我的选择器只有一个组件,但它可能有多个组件,例如带有日期和时间的 datepicker

You can even use this method outside the function that you were talking about if you keep an outlet of your pickerview

如果您保留pickerview的出口,您甚至可以在您正在谈论的功能之外使用此方法