views:

387

answers:

2

I have the AlertView working perfectly with a "cancelButtonTitle:@"Cancel"" and "otherButtonTitles:nil". My question is how to get other buttons.

When I only change the "otherButtonTitles:@"2nd Button"", then the iPhone simulator just crashes out of the app and into the homescreen.

+6  A: 

You want to end your method call like this:

... cancelButtonTitle:@"Cancel" otherButtonTitles:@"Button1Title", @"Button2Title", nil];

This is the same pattern you see in String formatting, where the list of arguments can be of any length. Usually the argument list is then nil-terminated. Don't forget the nil.

Kevin Conner
Didn't know about the nil. Works perfectly, thanks!
Devoted
+3  A: 

Exactly like Kevin said, but as an addendum to that, you can also assign target-actions to the other buttons.

When you instantiate the UIAlertView, set the delegate argument to self, then add the following method to your object:

-(void) alertView: ( UIAlertView *) alertView 
         clickedButtonAtIndex: ( NSInteger ) buttonIndex {
      // do stuff
      // if you want the alert to close, just call [ alertView release ]   
}

`

Jacob Relkin
How do I control which button will activate this method though? I tried it out and no matter which button I press (including Cancel), it will always run this method. Ohhh! Do I set a conditional inside this method to check which button to do stuff for?
Devoted
Yes. You got it.
Jacob Relkin
Like `[ self performSelector: [ buttonActions objectAtIndex: buttonIndex ] ];`This would work if you had an array of selectors called 'buttonActions' -- just an example.
Jacob Relkin
makes sense, thank you
Devoted
why would you bother with that, instead of of using switch(buttonIndex) in the delegate callback?
executor21
! and how would you do that?
Devoted
switch(buttonIndex){ case 0: [self oneMethod]; break; case 1: [self anotherMethod]; break; default: break;}
executor21
How do I make it look like code in the comments?
executor21
use the \`at the beginning and a \` at the end
Jacob Relkin
( i escaped those so u could see them ).
Jacob Relkin
and, executor21, it was just an example, i know it could work that way, but my example is quite a bit more dynamic with less code.
Jacob Relkin