How To Get Selected Value From UIPickerView

IosObjective CCocoa Touch

Ios Problem Overview


I know that with a UIDatePicker, you can use something like:

NSDate *myDate = picker.date;

But I am using a UIPickerView in my view. How can i similarly get the value selected? Or do I have to setup didSelectRow type of method to do this?

Update: This code works for picker with 1 component:

NSInteger row;
NSString *weightSelected;
    
row = [repPicker selectedRowInComponent:0];
weightSelected = [pickerArray objectAtIndex:row];

I tired this code for my picker with 2 components, but it is freezing:

NSInteger row1, row2;
NSString *weightSelected1;
NSString *weightSelected2;
    
row1 = [repPicker selectedRowInComponent:0];
row2 = [repPicker selectedRowInComponent:1];
weightSelected1 = [pickerArray objectAtIndex:row1];
weightSelected2 = [pickerArray objectAtIndex:row2];
NSString *weightSelected = [NSString stringWithFormat:@"%@.%@", weightSelected1, weightSelected2];

Ios Solutions


Solution 1 - Ios

You can get it in the following manner:

NSInteger row;
NSArray *repeatPickerData;
UIPickerView *repeatPickerView;

row = [repeatPickerView selectedRowInComponent:0];
self.strPrintRepeat = [repeatPickerData objectAtIndex:row];

Solution 2 - Ios

You can get the text of the selected item in any section of the picker using the same function that the pickerView does, from your custom ViewController class:

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

using the selected item

[myPickerView selectedRowInComponent:0]

so to set the text of a label in a custom cell using the currently selected value from the 2nd section of myPickerView (a property of your view controller probably)

[cell.label setText:[self pickerView:myPickerView titleForRow:[myPickerView selectedRowInComponent:1] forComponent:1]];

Just change both :1s to :2s, etc for each section

Solution 3 - Ios

This is what I did:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    selectedEntry = [allEntries objectAtIndex:row];
    
}

The selectedEntry is a NSString and will hold the currently selected entry in the pickerview. I am new to objective C but I think this is much easier.

Solution 4 - Ios

You can access the selected row for a given component using the following method:

- (NSInteger)selectedRowInComponent:(NSInteger)component

Otherwise, implementing the delegate function is the only other option.

Solution 5 - Ios

You will need to ask the picker's delegate, in the same way your application does. Here is how I do it from within my UIPickerViewDelegate:

func selectedRowValue(picker : UIPickerView, ic : Int) -> String {
    
    //Row Index
    let ir  = picker.selectedRow(inComponent: ic);
    
    //Value
    let val = self.pickerView(picker,
                              titleForRow:  ir,
                              forComponent: ic);
    return val!;
}

Solution 6 - Ios

You have to use the didSelectRow delegate method, because a UIPickerView can have an arbitrary number of components. There is no "objectValue" or anything like that, because that's entirely up to you.

Solution 7 - Ios

Getting the selected title of a picker:

let component = 0
let row = picker.selectedRow(inComponent: component)
let title = picker.delegate?.pickerView?(picker, titleForRow: row, forComponent: component)

Solution 8 - Ios

       NSInteger SelectedRow;
                   SelectedRow = [yourPickerView selectedRowInComponent:0];
                   selectedPickerString = [YourPickerArray objectAtIndex:SelectedRow];
                   self.YourTextField.text= selectedPickerString;
    
    // if  you want to move pickerview to selected row then 
 for (int i = 0; I<YourPickerArray.count; i++) {
  if ([[YourPickerArray objectAtIndex:i] isEqualToString:self.YourTextField.text]) { 
[yourPickerView selectRow:i inComponent:0 animated:NO];
 }
}

Solution 9 - Ios

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
   weightSelected = [pickerArray objectAtIndex:row];
}

Solution 10 - Ios

This is my answer

- (IBAction)Result:(id)sender 
{
   self.statusLabel.text = DataSource[[pickerViewTool selectedRowInComponent:0]];

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser594161View Question on Stackoverflow
Solution 1 - IosSJSView Answer on Stackoverflow
Solution 2 - Iosnh32rgView Answer on Stackoverflow
Solution 3 - IosHarikrishnanView Answer on Stackoverflow
Solution 4 - IosdrewagView Answer on Stackoverflow
Solution 5 - IosJ-DizzleView Answer on Stackoverflow
Solution 6 - IosDave DeLongView Answer on Stackoverflow
Solution 7 - IospkambView Answer on Stackoverflow
Solution 8 - Iosmaan89View Answer on Stackoverflow
Solution 9 - IoscHELSEAView Answer on Stackoverflow
Solution 10 - IosAhmad AlwisiView Answer on Stackoverflow