How to fetch the currently selected row in the uipickerview when tapped Done ( having multiple pickers)

12,665

Solution 1

//Suppose you have created three picker views as firstPickerView, secondPickerView and ThirdPickerView

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if(pickerView == firstPickerView)
    {
        NSString *strFirstPickerView = [yourarray objectAtIndex:row];
    }
    else if(pickerView == secondPickerView)
    {
        NSString *strSecondPickerView = [yourarray objectAtIndex:row];
    }
    else
    {
        NSString *strThirdPickerView = [yourarray objectAtIndex:row];
    }
}

Solution 2

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

now str have a selected value. so you can use it when done button press.

Solution 3

selectedRowInComponent:

- Returns the index of the selected row in a given component.
- (NSInteger)selectedRowInComponent:(NSInteger)component

Parameters component A zero-indexed number identifying a component of the picker view. Return Value A zero-indexed number identifying the selected row, or -1 if no row is selected.

For Help

And to make a difference between different pickerViews add tag to each of the pickers:

 self.pikcerView.tag = 1;

Hope this helps.

Share:
12,665
user198725878
Author by

user198725878

iOs developer

Updated on June 18, 2022

Comments

  • user198725878
    user198725878 about 2 years

    I have got UIPickerview in my application working fine.

    I want to change the default selection behavior of the UIPickerview, i mean i have a done button in the UIToolbar. The toolbar is added to the UIPickerview.

    Whenever the user taps on the "Done" button, I want to consider the highlighted entry as selected. That means user can move up and down , when the user taps on the "Done" button, i want to consider the selection now

    Please let me know , how can I fetch the currently selected row for the UIPickerview when tapped "Done" button

    Note: I have 3 pickers in my view ( i mean i want to have pickerview tag and pickerview selected row)