Use NSArray to specify otherButtonTitles?

http://stackoverflow.com/questions/1602214/use-nsarray-to-specify-otherbuttontitles

UIAlertSheet's constructor takes an otherButtonTitles parameter as a varg list. I'd like to specify the other button titles from an NSArray instead. Is this possible?

i.e. I have to do this:

id alert = [[UIActionSheet alloc] initWithTitle: titleString
                                  delegate: self
                                  cancelButtonTitle: cancelString
                                  destructiveButtonTitle: nil
                                  otherButtonTitles: button1Title, button2Title, nil];

But since I'm generating the list of available buttons at runtime, I really want something like this:

id alert = [[UIActionSheet alloc] initWithTitle: titleString
                                       delegate: self
                              cancelButtonTitle: cancelString
                         destructiveButtonTitle: nil
                              otherButtonTitles: otherButtonTitles];

Right now, I'm thinking that I need to have a seperate call to initWithTitle: for 1 item, 2 items and 3 items. Like this:

if ( [titles count] == 1 ) {
     alert = [[UIActionSheet alloc] initWithTitle: titleString
                                         delegate: self
                                cancelButtonTitle: cancelString
                           destructiveButtonTitle: nil
                                otherButtonTitles: [titles objectAtIndex: 0], nil];
} else if ( [titles count] == 2) {
     alert = [[UIActionSheet alloc] initWithTitle: titleString
                                         delegate: self
                                cancelButtonTitle: cancelString
                           destructiveButtonTitle: nil
                                otherButtonTitles: [titles objectAtIndex: 0], [titles objectAtIndex: 1],  nil];
} else {
    // and so on
}

That's a lot of duplicate code, but it might actually be reasonable since I have at most three buttons. How can I avoid this?

shareimprove this question
 

6 Answers

This is a year old but the solution is pretty simple ... do as @Simon suggested but do not specify a cancel button title, so:

UIActionSheet *alert = [[UIActionSheet alloc] initWithTitle: titleString
                              delegate: self
                              cancelButtonTitle: nil
                              destructiveButtonTitle: nil
                              otherButtonTitles: nil];

But after adding your normal buttons, add the cancel button, like:

for( NSString *title in titles)  {
    [alert addButtonWithTitle:title]; 
}

[alert addButtonWithTitle:cancelString];

Now the key step is to specify which button is the cancel button, like:

sheet.cancelButtonIndex = [titles count];

We do [titles count] and not [titles count] - 1 because we are adding the cancel button as extra from the list of buttons in titles.

You now also specify which button you want to be the destructive button (ie the red button) by specifying the destructiveButtonIndex (typically that will be the [titles count] - 1 button). Also, if you keep the cancel button to be the last button, iOS will add that nice spacing between the other buttons and the cancel button.

All of these is iOS 2.0 compatible so enjoy.

shareimprove this answer
 
    
Not sure why it worked for you, but I had to do "[titles count] - 1" for it to work for me, iOS 7. –  Micah Mar 28 '14 at 18:38
    
[alert numberOfButtons]-1 is another way to set the cancel button's index –  Keith Apr 3 '14 at 16:42 
    
Minor note: Using the names you've established here, I think sheet.cancelButtonIndex should bealert.cancelButtonIndex, yes? –  Matt May 15 '14 at 23:59
 

Instead of adding the buttons when you initialize the UIActionSheet, try adding them with the addButtonWithTitle method using a for loop that goes through your NSArray.

UIActionSheet *alert = [[UIActionSheet alloc] initWithTitle: titleString
                              delegate: self
                              cancelButtonTitle: cancelString
                              destructiveButtonTitle: nil
                              otherButtonTitles: nil];

for( NSString *title in titles)  
    [alert addButtonWithTitle:title];
shareimprove this answer
 
12  
easier than that: for (NSString * title in titles) { [alert addButtonWithTitle:title]; }There's no need to create an enumerator. –  Dave DeLong Oct 21 '09 at 17:28
3  
This makes the buttons appear AFTER the cancel button. Any way to fix this @bent or @simon? (see edit on stackoverflow.com/questions/7781022) –  Micah Hainline Oct 17 '11 at 23:38
1  
Try using the cancelButtonIndex property. –  Simon Nov 23 '11 at 20:53
1  
@MicahHainline - just add this line at the end of the 'for' loop: [actionSheet addButtonWithTitle:@"Cancel_Button_Title"]; actionSheet.cancelButtonIndex = actionSheet.numberOfButtons-1; –  Sagiftw Mar 18 '12 at 18:26 
    
This adds the buttons under the cancel button for me. –  jspooner Apr 12 '12 at 2:12

addButtonWithTitle: returns the index of the added button. Set cancelButtonTitle to nil in the init method and after adding additional buttons run this:

actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];
shareimprove this answer
 
- (void)showActionSheetWithButtons:(NSArray *)buttons withTitle:(NSString *)title {

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle: title 
                                                             delegate: self
                                                    cancelButtonTitle: nil 
                                               destructiveButtonTitle: nil 
                                                    otherButtonTitles: nil];

    for (NSString *title in buttons) {
        [actionSheet addButtonWithTitle: title];
    }

    [actionSheet addButtonWithTitle: @"Cancel"];
    [actionSheet setCancelButtonIndex: [buttons count]];
    [actionSheet showInView:self.view];
}
shareimprove this answer
 

You can add the cancel button and set it like this:

[actionSheet setCancelButtonIndex: [actionSheet addButtonWithTitle: @"Cancel"]];
shareimprove this answer
 

I know this is an old post, but in case someone else, like me, is trying to figure this out.

(This WAS answered by @kokemomuke. This is mostly a more detailed explanation. Also building on @Ephraim and @Simon)

It turns out the LAST entry of addButtonWithTitle: needs to be the Cancel button. I'd use:

// All titles EXCLUDING Cancel button
for( NSString *title in titles)  
    [sheet addButtonWithTitle:title];


// The next two line MUST be set correctly: 
// 1. Cancel button must be added as the last entry
// 2. Index of the Cancel button must be set to the last entry

[sheet addButtonWithTitle:@"Cancel"];

sheet.cancelButtonIndex = titles.count - 1;
shareimprove this answer
 

posted on 2015-03-09 15:35  沉淀2014  阅读(262)  评论(0编辑  收藏  举报

导航