Search code examples
iosobjective-cuipickerview

Get currently selected value of UIPickerView


I have been googling how to get the currently selected value in UIPickerView, but everywhere it's mentioned using row:0 and so on.

I have a UIPickerView populated with string values like "One","Two", etc.

Now when I select a value to let's say "Two", is there anyway I can get this text.

Like in UITextView where it is _textview.text


Solution

  • Every UIPickerView should have a delegate.

    And you can ask this delegate for your picker's selected row title via something like:

      UIPickerViewDelegate *delegate = yourPickerView.delegate;
      NSString *titleYouWant = [delegate pickerView:yourPickerView titleForRow:[yourPickerView selectedRowInComponent:0] forComponent:0];
    

    (This is ASSUMING your component number is zero; your own implementation might be different).

    More documentation on the "pickerView:titleForRow:forComponent" method can be seen here.