Use NSArray to specify otherButtonTitles?

The UIAlertSheet constructor accepts the otherButtonTitles parameter as a varg list. Instead, I would like to specify other button names from NSArray. Is it possible?

i.e. I must do it:

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

But since I am generating a 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 think I need to have a separate initWithTitle: call 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 } 

This is a lot of duplicate codes, but it might be wise, since I have no more than three buttons. How can i avoid this?

+54
iphone nsarray uialertsheet
Oct 21 '09 at 17:10
source share
6 answers

This is a year, but the solution is quite simple ... do as @Simon suggested, but do not specify the name of the cancel button, therefore:

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

But after adding the usual buttons, add a cancel button, for example:

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

Now the key step is to indicate which button is the cancel button, for example:

 alert.cancelButtonIndex = [titles count]; 

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

Now you also indicate which button you want to be a destructive button (that is, a red button) by specifying destructiveButtonIndex (usually this is the [titles count] - 1 button). In addition, if you leave the cancel button with the last button, iOS will add this good distance between the other buttons and the cancel button.

They are all compatible with iOS 2.0, so enjoy.

+72
Nov 28 '12 at 22:05
source share

Instead of adding buttons when initializing UIActionSheet, try adding them using the addButtonWithTitle method using the for loop that passes through NSArray.

 UIActionSheet *alert = [[UIActionSheet alloc] initWithTitle: titleString delegate: self cancelButtonTitle: cancelString destructiveButtonTitle: nil otherButtonTitles: nil]; for( NSString *title in titles) [alert addButtonWithTitle:title]; 
+52
Oct 21 '09 at 17:23
source share

addButtonWithTitle: returns the index of the added button. Set the cancelButtonTitle parameter to nil in the init method, and after adding additional buttons, do the following:

 actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"]; 
+6
Aug 24 '13 at 5:21
source share
 - (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]; } 
+4
Dec 25 '12 at 7:28
source share

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

 [actionSheet setCancelButtonIndex: [actionSheet addButtonWithTitle: @"Cancel"]]; 
+2
Feb 18 '14 at 17:06
source share

I know this is an old post, but if someone, like me, is trying to figure it out.

(This was caused by @kokemomuke. This is basically a more detailed explanation. Also, based on @Ephraim and @Simon)

It turns out that the LAST record addButtonWithTitle: should have a Cancel button. I would 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; 
+1
Jun 17 '14 at 7:05
source share



All Articles