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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With