Get currently selected value of UIPickerView

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)


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.


func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
    // selected value in Uipickerview in Swift
    let value=array[row]
    println("values:----------\(value)");

}

First of all add UIPickerViewDatasource and UIPickerViewDelegate in .h file

then in .m add the line

self.myPickerView.delegate = self;

now assign array by its objects.

NSArray *arrOfAge = [[NSArray alloc]initWithObjects:@“one”,@”two”, nil];

This are delegate methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [arrOfAge count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSString *titleRow;
    titleRow = [NSString stringWithFormat:@"%@", [arrOfAge objectAtIndex:row]];
    return titleRow;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    selectedRow = row;
}

This IBAction btnDone returns the selected row from pickerView.

- (IBAction)btnDone:(id)sender
{
    NSUInteger num = [[self.myPickerView dataSource] numberOfComponentsInPickerView:self.myPickerView];
    NSMutableString *text = [NSMutableString string];
    for(NSUInteger i =0;i<num;++i)
    {
        NSUInteger selectRow = [self.myPickerView selectedRowInComponent:i];
        NSString *ww = [[self.myPickerView delegate] pickerView:self.myPickerView titleForRow:selectRow forComponent:i];
        [text appendFormat:@"%@",ww];
        NSLog(@"%@",text);
    }
}