How can I sent an array of strings into an UIActionSheet varargs init method?

三世轮回 提交于 2019-12-04 04:20:28
Himanshu A Jadav

You can try this

NSMutableArray *buttonTitles = [NSMutableArray array];
if (condition1) {
    [buttonTitles addObject: @"Do action 1"];
}
if (condition2) {
    [buttonTitles addObject: @"Do action 2"];
}
if (condition3) {
    [buttonTitles addObject: @"Do action 3"];
}
if (condition4) {
    [buttonTitles addObject: @"Do action 4"];
}
[buttonTitles addObject: @"Cancel"];
UIActionSheet *actionSheet = [[[UIActionSheet alloc] initWithTitle: nil delegate: self cancelButtonTitle: nil destructiveButtonTitle: nil otherButtonTitles: nil] autorelease];

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

[actionSheet setCancelButtonIndex: [buttonTitles count] - 1];

A variation on Himanshu's answer:

    UIActionSheet * as = [[UIActionSheet alloc] initWithTitle:@"Share" 
                                                 delegate:self 
                                        cancelButtonTitle:nil /* don't set Cancel title here! */
                                   destructiveButtonTitle:nil 
                                        otherButtonTitles:nil];
int cancelButtonIndex = 0; // will remain 0 if no other buttons besides "Cancel"
if ( canShareBySMS )
{
    [as addButtonWithTitle:kSMSButtonText];
    cancelButtonIndex++;
}
if ( canShareByMail )
{
    [as addButtonWithTitle:kEmailButtonText];
    cancelButtonIndex++;
}
/* etc. */

// after all other buttons have been added, include Cancel
[as addButtonWithTitle:@"Cancel"];
[as setCancelButtonIndex:cancelButtonIndex];

[as showInView:self.sharingViewController.view];
[as release];

Note: your UIActionSheetDelegate method should check against button titles instead of buttonIndex:

- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex {
    if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:kSMSButtonText]) {
    //share via sms
    }else if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:kEmailButtonText]) {
    //share via mail
    }
}

I think the key detail people miss is setting the cancelButton in the initWithTitle selector, instead of adding it after all the other buttons and specifying the look of the cancel button by calling setCancelButtonIndex:.

Why not just do something like this:

for (Nstring *button in buttonTitles)
  [actionSheet addButtonWithTitle:button];

Here is a method I tried that seems to work just fine. This works if you want to add an additional button at the end of the buttons, before the "cancel" button.

NSString * optionalButton = nil;
if (condition4) {
  optionalButton = @"Some Additional Option";
}

UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:@"Some Title" 
                                                          delegate:self 
                                                 cancelButtonTitle:@"Cancel" 
                                            destructiveButtonTitle:nil 
                                                 otherButtonTitles:@"Button 1",@"Button 2",@"Button 3",optionalButton, nil];


[actionSheet showInView:self.view];

It appears to be okay to add an extra 'nil' at the end of the buttons list if your condition (condition4) is not met.

I've successfully tested this on iOS7.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!