Use NSArray to specify otherButtonTitles?
Solution 1:
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.
Solution 2:
Instead of adding the buttons when you initialize the UIActionSheet, try adding them with the addButtonWithTitle method using a for loop that goes through your NSArray.
UIActionSheet *alert = [[UIActionSheet alloc] initWithTitle: titleString
delegate: self
cancelButtonTitle: cancelString
destructiveButtonTitle: nil
otherButtonTitles: nil];
for( NSString *title in titles)
[alert addButtonWithTitle:title];
Solution 3:
addButtonWithTitle: returns the index of the added button. Set cancelButtonTitle to nil in the init method and after adding additional buttons run this:
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];