Use NSArray to specify otherButtonTitles?
http://stackoverflow.com/questions/1602214/use-nsarray-to-specify-otherbuttontitles
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:
But since I'm generating the list of available buttons at runtime, I really want something like this:
Right now, I'm thinking that I need to have a seperate call to
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? |
|||
add a comment
|
This is a year old but the solution is pretty simple ... do as @Simon suggested but do not specify a cancel button title, so:
But after adding your normal buttons, add the cancel button, like:
Now the key step is to specify which button is the cancel button, like:
We do 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 All of these is iOS 2.0 compatible so enjoy. |
|||||||||||||
add a comment
|
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.
|
|||||||||||||||||||||
add a comment
|
addButtonWithTitle: returns the index of the added button. Set cancelButtonTitle to nil in the init method and after adding additional buttons run this:
|
|||
add a comment
|
|
||||
add a comment
|
You can add the cancel button and set it like this:
|
|||
add a comment
|
I know this is an old post, but in case someone else, like me, is trying to figure this out. (This WAS answered by @kokemomuke. This is mostly a more detailed explanation. Also building on @Ephraim and @Simon) It turns out the LAST entry of addButtonWithTitle: needs to be the
|
|||
add a comment
|