How to add a button to an existing UIActionSheet?
-
19-09-2019 - |
Question
I have this code:
UIActionSheet *actionSheet = [[[UIActionSheet alloc]
initWithTitle:@"Illustrations"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles: @"ABC", @"XYZ",
nil] autorelease];
UIImage *image = // whatever, snip
if (image != nil)
{
[actionSheet addButtonWithTitle:@"LMNOP"];
}
and it does a great job of adding my LMNOP button conditionally.
...AFTER the cancel button.
How can I construct my action sheet with a conditional button? Sadly, I can't do:
UIActionSheet *actionSheet = [[[UIActionSheet alloc]
// ... etc.
otherButtonTitles: someMutableArray
// ... etc.
because that would certainly help.
Any ideas?
Thanks!
Solution
You can add all buttons after the init method.
UIActionSheet* sheet = [[[UIActionSheet alloc] init] autorelease];
sheet.title = @"Illustrations";
sheet.delegate = self;
[sheet addButtonWithTitle:@"ABC"];
[sheet addButtonWithTitle:@"XYZ"];
if (condition)
[sheet addButtonWithTitle:@"LMNOP"];
sheet.cancelButtonIndex = [sheet addButtonWithTitle:@"Cancel"];
OTHER TIPS
im coding in for iOS 4 and this is the method that is used. You just add the title you want for the button in the otherbutton sections.
UIActionSheet *phoneActionSheet = [[UIActionSheet alloc]
initWithTitle:@"Do you want to call or text this person?"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:@"Call"
otherButtonTitles:@"Text",nil];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow