Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 844
rbk Avatar asked Apr 18 '14 07:04

rbk


2 Answers

Swift 3

you can use this code for getting current title

self.textview.text = self.pickerView(self.pickerViewOutlet, titleForRow: self.pickerViewOutlet.selectedRow(inComponent: 0), forComponent: 0)
like image 50
Sachin Nautiyal Avatar answered Oct 27 '22 00:10

Sachin Nautiyal


If the UIPickerView has only one component (something similar to section for UITableView) you can retrieve the "index" selected with the following line:

NSInteger indexSelected = [myPicker selectedRowInComponent:0];

(change the parameter 0 if you have more than one component).

This is the index in your array of data (myArray), the same you used to return data from the delegate method, as in:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
 return myArray[row];
}

(or similar delegate methods -attributedTitleForRow -viewForRow)

like image 39
valfer Avatar answered Oct 27 '22 01:10

valfer