Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a button to an existing UIActionSheet?

Tags:

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!

like image 989
Olie Avatar asked Mar 11 '10 02:03

Olie


1 Answers

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"];
like image 61
kennytm Avatar answered Oct 13 '22 07:10

kennytm