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
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With