Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone SDK - UIActionSheet - Dynamic Button Titles

Tags:

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.

like image 694
harrywynn Avatar asked Oct 26 '09 15:10

harrywynn


1 Answers

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.

like image 100
jhabbott Avatar answered Oct 19 '22 05:10

jhabbott