Create UIActionSheet 'otherButtons' by passing in array, not varlist Create UIActionSheet 'otherButtons' by passing in array, not varlist ios ios

Create UIActionSheet 'otherButtons' by passing in array, not varlist


I got this to work (you just need to, be ok with a regular button, and just add it after :

NSArray *array = @[@"1st Button",@"2nd Button",@"3rd Button",@"4th Button"];    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Title Here"                                                             delegate:self                                                    cancelButtonTitle:nil                                               destructiveButtonTitle:nil                                                    otherButtonTitles:nil];    // ObjC Fast Enumeration    for (NSString *title in array) {        [actionSheet addButtonWithTitle:title];    }    actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];    [actionSheet showInView:self.view];


One little note: [actionSheet addButtonWithTitle:] returns the index of that button, so to be safe and "clean" you can do this:

actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];


Taking Jaba's and Nick's answers and extending them a little further. To incorporate a destruction button into this solution:

// Create action sheetUIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title                                                         delegate:self                                                cancelButtonTitle:nil                                           destructiveButtonTitle:nil                                                otherButtonTitles:nil];// Action Buttonsfor (NSString *actionName in actionNames){    [actionSheet addButtonWithTitle: actionName];}// Destruction Buttonif (destructiveName.length > 0){    [actionSheet setDestructiveButtonIndex:[actionSheet addButtonWithTitle: destructiveName]];}// Cancel Button[actionSheet setCancelButtonIndex: [actionSheet addButtonWithTitle:@"Cancel"]];// Present Action Sheet[actionSheet showInView: self.view];