Create UIActionSheet 'otherButtons' by passing in array, not varlist
Solution 1
I got this to work (you just need to, be ok with a regular button, and just add it after :
NSArray *array = @[@"1st Button",@"2nd Button",@"3rd Button",@"4th Button"];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Title Here"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
// ObjC Fast Enumeration
for (NSString *title in array) {
[actionSheet addButtonWithTitle:title];
}
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];
[actionSheet showInView:self.view];
Solution 2
One little note: [actionSheet addButtonWithTitle:] returns the index of that button, so to be safe and "clean" you can do this:
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];
Solution 3
Taking Jaba's and Nick's answers and extending them a little further. To incorporate a destruction button into this solution:
// Create action sheet
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
// Action Buttons
for (NSString *actionName in actionNames){
[actionSheet addButtonWithTitle: actionName];
}
// Destruction Button
if (destructiveName.length > 0){
[actionSheet setDestructiveButtonIndex:[actionSheet addButtonWithTitle: destructiveName]];
}
// Cancel Button
[actionSheet setCancelButtonIndex: [actionSheet addButtonWithTitle:@"Cancel"]];
// Present Action Sheet
[actionSheet showInView: self.view];
Solution 4
There is the swift version for the response :
//array with button titles
private var values = ["Value 1", "Value 2", "Value 3"]
//create action sheet
let actionSheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: nil, destructiveButtonTitle: nil)
//for each value in array
for value in values{
//add a button
actionSheet.addButtonWithTitle(value as String)
}
//display action sheet
actionSheet.showInView(self.view)
To get value selected, add delegate to your ViewController :
class MyViewController: UIViewController, UIActionSheetDelegate
And implement the method "clickedButtonAtIndex"
func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
let selectedValue : String = values[buttonIndex]
}
Related videos on Youtube
![Greg Maletic](https://i.stack.imgur.com/r4iyd.png?s=256&g=1)
Greg Maletic
Work on Special Projects at Panic Inc. Developer of Wishing Stars, a GPS-based adventure you play at the Disney Parks. Also the producer of TILT: The Battle to Save Pinball, a documentary chronicling the decline of the pinball industry.
Updated on May 21, 2020Comments
-
Greg Maletic about 4 years
I have an array of strings that I want to use for button titles on a UIActionSheet. Unfortunately, the otherButtonTitles: argument in the method invocation takes a variable length list of strings, not an array.
So how I can I pass these titles into the UIActionSheet? The workaround I've seen suggested is to pass nil into otherButtonTitles:, then specify the button titles individually by using addButtonWithTitle:. But this has the problem of moving the "Cancel" button to the first position on the UIActionSheet rather than the last; I want it to be the last one.
Is there a way to 1) pass an array in lieu of a variable list of strings, or alternatively 2) move the cancel button to the bottom of the UIActionSheet?
Thanks.
-
Vaerenberg over 14 yearsnot sure if this will work but the cancelButtonIndex property of UIActionSheet is not read-only. Try setting it and see if that changes the order of the buttons?
-
-
Greg Maletic over 14 yearsThis works; thanks. Didn't realize that the cancelButtonIndex property could be set. One thing I think we can all agree on: Apple's API for this sucks.
-
Duck almost 14 yearsI second this! And 99,99% of their documentation also sucks. It is like trying to learn brain surgery from a book written in archaic hebrew without knowing that language.
-
mattjgalloway about 12 yearsI very much wish I could up vote this many many more times. Cracking answer and I cannot believe I've gone this long not knowing about this!
-
user4951 over 11 yearsWhat about if we want the cancel button to be red like usual?
-
user4951 over 11 yearsWill that turn the cancel button red?
-
Nick over 11 years@JimThio No, red button is called "destructiveButton", and there's a respective property
destructiveButtonIndex
for it -
Armin almost 11 years@JimThio
cancelButton
is not red. ThedestructiveButton
is red. -
Daniel Rinser almost 11 yearsSadly, this solution suffers from a bug in UIActionSheet that
[UIActionSheet addButtonWithTitle]
does not seem to correctly setfirstOtherButtonIndex
(which should be used in the delegate'sactionSheet:clickedButtonAtIndex:
rather than hardcoding 0 or similar stuff). Getting nasty with converting anNSArray
tova_list
doesn't sound like a great solution either. :-( This API is so damn ugly. -
José Manuel Sánchez over 10 yearsI used this recently showing the action sheet from a bar button item, and it seems to have a bug because the last item is not being shown. I had to add an empty button title to workaround this. I'm not sure if this is only on iOS 7
-
manuelwaldner over 8 yearsTo solve the problems with the indexes, i created a
NSDictionary
containing the titles as keys withNSNumber
(e.g.@(0)
) as values. InactionSheet:clickedButtonAtIndex:
for example i get the title for the specific pressedbuttonIndex
viabuttonTitleAtIndex
on theactionSheet
. Then i get the value with the title as key from my dictionary and take theintegerValue
of this value to do aswitch-case
. For me this is working great. Don't forget to add the all button titles also cancel etc. to your dictionary -
jeet.chanchawat over 5 yearsFor future users... It worked... but it is deprecated... use stackoverflow.com/a/41322537/884674