Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I know the UIPickerView has this method:

- (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?

like image 554
Bogdan Alexandru Avatar asked Sep 03 '13 15:09

Bogdan Alexandru


2 Answers

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

Such like

NSString *YourselectedTitle = [self.yourArrayName objectAtIndex:[self.yourPickerName selectedRowInComponent:0]];
NSLog(@"%@", YourselectedTitle);
like image 59
iPatel Avatar answered Nov 10 '22 01:11

iPatel


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

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

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

like image 5
Dani Avatar answered Nov 10 '22 01:11

Dani