Bogdan Alexandru
Bogdan Alexandru

Reputation: 5552

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?

Upvotes: 9

Views: 24218

Answers (2)

Dani
Dani

Reputation: 187

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

Upvotes: 5

iPatel
iPatel

Reputation: 47099

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);

Upvotes: 27

Related Questions