Button action changes with UIPickerView change

Discussion in 'iOS Programming' started by newtoiphonesdk, Oct 25, 2011.

  1. newtoiphonesdk macrumors 6502a

    Joined:
    Jul 30, 2010
    #1
    I have a ViewController with a UIPickerView loaded from a NSMutableArray. I also have a UIBarButtonItem for an IBAction. What I would like to do is have the IBAction do different things, depending on what is selected in the UIPickerView. I first tried this:
    Code:
    -(IBAction)compose {
        if ([options objectAtIndex:0]) {
            [self displayComposerSheet];
    
        } 
        if ([options objectAtIndex:1]) {
            
                [self displayComposerSheet2];
            }
           
        if ([options objectAtIndex:2]) {
            [self displayComposerSheet3];
        }
    }
    
    This doesn't change anything, and will only pull displayComposerSheet, and none of the others. If I try to change the if statement to:
    Code:
    if ([options objectAtIndex:row])
    I am informed that row has not been declared, though I set it up using NSInteger and void methods earlier. How can I get this one button to perform different actions based on what the uipickerview shows?
     
  2. larswik, Oct 26, 2011
    Last edited by a moderator: Oct 26, 2011

    larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #2
    I might not be the best person to help you since I am still learning but your if statements don't appear to do anything so it can't evaluate to TRUE or FALSE. Your saying if ([options objectAtIndex:0]) that seems to be the first part but the part you are evaluating it to is not there. It does not look like a BOOL but I could be wrong.

    Example.
    Code:
    BOOL car = TRUE;
    
    if (car){
       NSLog@"It's a car";
    }
    Or what you have should be something like this. I am not infront of Xcode so it might be a little off.
    Code:
    if ([options objectAtIndex:0] isEqualToString:@"Yoda"]) {
        do something;
    }
    or
    Code:
    if (a > b){
        do something;
    }
    Your just kind of saying
    Code:
    if (a){
    }
    and I don't think that is a BOOL value in which case nothing will happen since you didn't tell it to do anything.
     
  3. Shawnpk macrumors 6502

    Joined:
    Jan 13, 2011
    Location:
    Los Angeles, CA
    #3
    Instead of just "if" statements, try "if/else if" statements or even a "switch" statement.
     
  4. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    Code:
    if ([options objectAtIndex:0])
    This is true if there is an object at index 0 of options, regardless of whether the user has selected that object or not.

    What you actually want in that place is more along the lines of:

    Code:
    if ([options selectedRowInComponent:0] == 0)
    The selectedRowInComponent: method will return what the user has selected. You have to specify component 0, because though your picker may not utilize it, some pickers have multiple components. IE, a time picker has one component for selecting the hour and a second component for selecting the minute.

    Alternatively,

    Code:
    switch ([options selectedRowInComponent:0])
    {
    case 0: ...
    would work pretty well.
     

Share This Page