How to add a button to an existing UIActionSheet?
Asked Answered
O

2

20

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!

Onceover answered 11/3, 2010 at 2:42 Comment(0)
P
51

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"];
Phenolphthalein answered 11/3, 2010 at 7:7 Comment(1)
Aha! I had missed the sheet.cancelButtonIndex = ... part; that's what I needed to complete the picture. Thanks!Onceover
M
-4

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];
Mcguigan answered 1/12, 2011 at 21:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.