views:

724

answers:

3

hi iam creating an UIActionSheet with 2 buttons . now i want two each buttons do separate work . how can i declare my 2 buttons on the :

- (void)actionSheet:(UIActionSheet *)menu
                didDismissWithButtonIndex:(NSInteger)buttonIndex 

i use this code :

if (buttonIndex != [menu cancelButtonIndex])    {
    // do somthing
}

but it means if user click any button except CANCEL BUTTON do somthing . Thank you .

+1  A: 
- (void)actionSheet:(UIActionSheet *)menu didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex == [menu cancelButtonIndex]) {
        // do something because the user clicked "cancel".
    } else {
        // do something because the user clicked "the other button".
    }
}
KennyTM
doesn't any change ! :) what about button 2 ? hum ?
Momeks
@Momeks: So you mean you have *three* buttons :)
KennyTM
Dear . i have 3 button for example Button 1 / 2 / 3 / Cancel. Now what can i do >?
Momeks
+3  A: 

This will work much more generically. You can extend it to as many buttons as you like:

- (void)actionSheet:(UIActionSheet *)menu didDismissWithButtonIndex:(NSInteger)buttonIndex {

    switch (buttonIndex) {
        case 0:
            //do something
            break;
        case 1:
            //do something else
            break;
        default:
            break;
    }
}
David Kanarek
Thank you works great !
Momeks
but have problem ! for example if put an alertview on the case 0 i get some error !!! why ? http://freezpic.com/pics/5a582757ae023e8f55dfcbf535fdbaa3.jpg
Momeks
You've got an error before you declare the UIAlertView. Check the line above, it might be missing a semicolon or something similar.
David Kanarek
sorry chek the my answer ! why comment doesn't support code tag? my code is there
Momeks
I think you aren't allowed to open a case with a variable declaration. I don't know why. If I add an NSLog statement in there or even just a semicolon it works fine.
David Kanarek
`case` is a label, and a label can only precede a statement, not a declaration. Declarations aren't statements in C, which means they aren't in Objective-C, either. Put an empty statement (`;`) between the `case` label and declaration, or wrap the whole case in a compound statement (`{` … `}`).
Peter Hosey
Thanks for the explanation. I managed to figure it out, but the details are always much more interesting.
David Kanarek
Thank you peter and David . it works great :) i Love stackoverflow
Momeks
A: 
- (void)actionSheet:(UIActionSheet *)menu didDismissWithButtonIndex:(NSInteger)buttonIndex {

    switch (buttonIndex) {

        case 0:

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Title" 
message:@"hooo" 
delegate:self
cancelButtonTitle:@"boo"
otherButtonTitles:@"yoo"];

            [alert show];
            [alert release];

            break;
            case 1:
            self.view.backgroundColor = [UIColor redColor];
                break;

        default:
            break;
    }
}
Momeks