Reputation: 2676
I have a requirement in an application where I need to be able to add otherButtonTitles dynamically, dependent upon some BOOL switches that a user has specified in the settings. However, I can't seem to figure out how to go about doing this in the UIActionSheet initialization. I've tried to pass a NSString array (NSString[2]), and also a NSArray without any luck.
Any help here is greatly appreciated.
Upvotes: 21
Views: 9518
Reputation: 19281
The easiest way to do this that I have found is initially create your action sheet with no buttons, including no cancel or destructive button:
UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:@"Dynamic"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
Then add a load of buttons as needed:
if(buttonX)
{
[actionSheet addButtonWithTitle:@"Button X"];
}
if(buttonY)
{
[actionSheet addButtonWithTitle:@"Button Y"];
}
if(buttonZ)
{
[actionSheet addButtonWithTitle:@"Button Z"];
}
Then finally add the cancel button at the end and set the cancel button index:
[actionSheet addButtonWithTitle:@"Cancel"];
actionSheet.cancelButtonIndex = actionSheet.numberOfButtons - 1;
Of course you can add both a cancel button and/or a destructive button in this way.
Upvotes: 58
Reputation: 2676
I ended up solving this by using some nil strings and an array. I place the dynamic titles I need in an array, then loop through it and set the placeholder strings with as many titles as necessary. The placeholder strings are then passed to otherButtonTitles:
in the action sheet initialization. Being otherButtonTitles:
is terminated by nil, you can pass as many placeholder strings as necessary, as the first nil placeholder will terminate the rest.
// button titles
NSMutableArray *buttons = [[NSMutableArray alloc] init];
[buttons addObject:@"Button 1"];
[buttons addObject:@"Button 2"];
// placeholders
NSString *button0 = nil, *button1 = nil, *button2 = nil;
// put together the buttons
for (int x = 0; x < buttons.count; x++) {
switch (x) {
case 0:
button0 = [buttons objectAtIndex:x];
break;
case 1:
button1 = [buttons objectAtIndex:x];
break;
case 2:
button2 = [buttons objectAtIndex:x];
break;
}
}
// action sheet
UIActionSheet *option = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:button0, button1, button2, nil];
Hope this is helpful to others facing a similar dilemma.
Upvotes: 2
Reputation: 1288
If you need that many buttons, create your own modal view and your own delegate protocol.
Check the documentation for presentModalViewController:animated
and dismissModalViewController:animated:
When the user dismisses your modal view, your delegate can receive a method you build, something like customActionSheetDidFinish:(int)buttonChosen
Upvotes: 1
Reputation: 170829
You can add new buttons to the (already initialized) UIActionSheet with addButtonWithTitle:
method. You can also create your custom UIButtons and add them to UIActionSheet's view as a subViews
Upvotes: 6