Use NSArray to specify otherButtonTitles?

Steven Fisher picture Steven Fisher · Oct 21, 2009 · Viewed 18.8k times · Source

UIAlertSheet's constructor takes an otherButtonTitles parameter as a varg list. I'd like to specify the other button titles from an NSArray instead. Is this possible?

i.e. I have to do this:

id alert = [[UIActionSheet alloc] initWithTitle: titleString
                                  delegate: self
                                  cancelButtonTitle: cancelString
                                  destructiveButtonTitle: nil
                                  otherButtonTitles: button1Title, button2Title, nil];

But since I'm generating the list of available buttons at runtime, I really want something like this:

id alert = [[UIActionSheet alloc] initWithTitle: titleString
                                       delegate: self
                              cancelButtonTitle: cancelString
                         destructiveButtonTitle: nil
                              otherButtonTitles: otherButtonTitles];

Right now, I'm thinking that I need to have a seperate call to initWithTitle: for 1 item, 2 items and 3 items. Like this:

if ( [titles count] == 1 ) {
     alert = [[UIActionSheet alloc] initWithTitle: titleString
                                         delegate: self
                                cancelButtonTitle: cancelString
                           destructiveButtonTitle: nil
                                otherButtonTitles: [titles objectAtIndex: 0], nil];
} else if ( [titles count] == 2) {
     alert = [[UIActionSheet alloc] initWithTitle: titleString
                                         delegate: self
                                cancelButtonTitle: cancelString
                           destructiveButtonTitle: nil
                                otherButtonTitles: [titles objectAtIndex: 0], [titles objectAtIndex: 1],  nil];
} else {
    // and so on
}

That's a lot of duplicate code, but it might actually be reasonable since I have at most three buttons. How can I avoid this?

Answer

Ephraim picture Ephraim · Nov 28, 2012

This is a year old but the solution is pretty simple ... do as @Simon suggested but do not specify a cancel button title, so:

UIActionSheet *alert = [[UIActionSheet alloc] initWithTitle: titleString
                              delegate: self
                              cancelButtonTitle: nil
                              destructiveButtonTitle: nil
                              otherButtonTitles: nil];

But after adding your normal buttons, add the cancel button, like:

for( NSString *title in titles)  {
    [alert addButtonWithTitle:title]; 
}

[alert addButtonWithTitle:cancelString];

Now the key step is to specify which button is the cancel button, like:

alert.cancelButtonIndex = [titles count];

We do [titles count] and not [titles count] - 1 because we are adding the cancel button as extra from the list of buttons in titles.

You now also specify which button you want to be the destructive button (ie the red button) by specifying the destructiveButtonIndex (typically that will be the [titles count] - 1 button). Also, if you keep the cancel button to be the last button, iOS will add that nice spacing between the other buttons and the cancel button.

All of these is iOS 2.0 compatible so enjoy.