Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

newtoiphonesdk

macrumors 6502a
Original poster
Jul 30, 2010
567
2
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?
 
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.
 
Last edited by a moderator:
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?

Instead of just "if" statements, try "if/else if" statements or even a "switch" statement.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.