Best way to extract selected item from pickerView

Discussion in 'iOS Programming' started by 1458279, Dec 6, 2011.

  1. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #1
    Here's the layout:
    I have a pickerView that has strings in it. I have a button and a textView. When the user selects the button, I add the selected string from the pickerView to the textView.

    I see several examples of getting a string from a pickerView. However, they store it to another string and grab that string.

    I can do that, but I should be able to grab the currently selected item from the pickerView.

    I see
    Code:
    - (NSInteger)selectedRowInComponent:(NSInteger)component
    Gives me the currently selected row.

    and
    Code:
    - (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent:(NSInteger)component;
    
    gives me the string that I want.

    So I should be able to do this:
    Code:
        int row = [myPicker1 selectedRowInComponent:0];
    
    NSString *myString = pickerView: myPicker1 titleForRow: row forComponent: 0;
    I get the current row, but I can't seem to get the string from the 'titleForRow' method.

    I know this is a noob question, but I've been up all night banging on this program and can't think straight.

    Thanks!
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    What data source are you using to back your pickerView?
     
  3. 1458279, Dec 6, 2011
    Last edited: Dec 6, 2011

    1458279 thread starter Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #3
    The data is a multi-dim array and nothing is done in IB, it's all coded.

    The example code I found, sets a string to the value in the titleForRow: method. I don't want to do it that way.

    The other sample is from the Apple 'Recipes' sample code, where it converts ounces/pounds ... The problem with their's is that they just get the position and that happens to be the number they want, I want the string:
    Code:
       NSInteger ounces = [pickerView selectedRowInComponent:OUNCES_COMPONENT];
    
    Code:
        NSArray *array = [[NSArray alloc] initWithObjects:@"Create",@"Use",@"Drop",@"iMac",@"Mac", @"iBook",@"Safari",@"iWont",@"iWill",@"iDid",@"iCould",nil];
        NSArray *array1 = [[NSArray alloc] initWithObjects:@"Objects", @"Heights", @"Values", nil];
        NSArray *arrayX = [[NSArray alloc] initWithObjects:array,array1, nil];
        self.pickerData = arrayX;
        myPicker1.hidden = NO;
        myPicker1.delegate = self;
        myPicker1.dataSource = self;
        myPicker1.showsSelectionIndicator = YES;
        myPicker1.contentMode = UIViewContentModeScaleAspectFit;
        myPicker1.autoresizingMask = UIViewAutoresizingFlexibleRightMargin  |UIViewAutoresizingFlexibleTopMargin;
        
        [self.view addSubview:myPicker1];
        [myPicker1 release];
    
    When the picker is being used, this is called:
    Code:
    - (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent:(NSInteger)component;
    {
        return [[pickerData objectAtIndex:component] objectAtIndex:row];
    }
    
    I have the row, and know which component, so I should be able to call 'titleForRow' and get the string back.
     
  4. RonC macrumors regular

    Joined:
    Oct 18, 2007
    Location:
    Chicago-area
    #4
    What is pickerData? Is that the same as self.pickerData? Presuming that it is, are the values for row and component correct? The debugger is your friend, use it!
     
  5. 1458279 thread starter Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #5
    yes pickerData and self.pickerData are the same and row and component are correct.

    The picker actually work correctly. What I'm trying to do here is have another object retrieve the information from the picker. From within the picker methods I am able to access the information and display it, but I want another object to have access to the data in the picker.

    Example, you have a picker that works just fine, displays it's data correctly. Then you have a button the wants to have access to the currently selected picker item.

    The examples I've seen, store the currently selected picker item in a separate object like a string or label or textview. I want to skip this separate object and access the currently selected picker item from another object.

    I have the row and component, just need to know how to call the picker object and have it return the currently selected item.
     
  6. 1458279, Dec 7, 2011
    Last edited: Dec 7, 2011

    1458279 thread starter Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #6
    Ok solved the problem:

    I was trying to use this:

    Code:
    NSString *myString = [self.myPicker1 titleForRow: row forComponent: 0];   
    It works when I use this:
    Code:
    NSString *myString = [[self.pickerData objectAtIndex:0] objectAtIndex:row];
    
    The 'titleForRow' method of the object returns the string from the second section of code ([[self.pickerData...

    So instead of calling 'titleForRow' I called the method that is the return of 'titleForRow'

    Still not sure how to call 'titleForRow'

    Anyways, thanks for the help, problem solved for now... on to the next problem :eek:
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    You don't need to, really. It's a delegate method for the UIPickerView. It's your job to provide the implementation of it.
     

Share This Page