我怎样才能将一个string数组发送到UIActionSheet的varargs初始化方法?

我有一个行动表,其中的选项取决于具体情况。 有足够多不同的button标题,我想先构造一个这些button标题的数组,但我不知道如何将其转换为可变参数格式。

我想要做这样的事情:

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"]; } UIActionSheet *actionSheet = [[[UIActionSheet alloc] initWithTitle: nil delegate: self cancelButtonTitle: @"Cancel" destructiveButtonTitle: nil otherButtonTitles: buttonTitles] autorelease]; 

现在显然,如果我不得不这样做,我可以做这样的事情:

 UIActionSheet *actionSheet = nil; if (condition1 && condition2 && condition3 && condition4) { actionSheet = [[[UIActionSheet alloc] initWithTitle: nil delegate: self cancelButtonTitle: @"Cancel" destructiveButtonTitle: nil otherButtonTitles: @"Do action1", @"Do action2", @"Do action3", @"Do action 4", nil] autorelease]; } else if (condition1 && condition2 && condition3 && !condition4) { actionSheet = [[[UIActionSheet alloc] initWithTitle: nil delegate: self cancelButtonTitle: @"Cancel" destructiveButtonTitle: nil otherButtonTitles: @"Do action1", @"Do action2", @"Do action3", nil] autorelease]; } // else ... // about 14 other cases! 

但是这太可怕了。 任何人都知道一些很好的语法糖来帮助我?

编辑:有人build议,我使用addButtonWithTitle ,它的表面看起来不错,不幸的是,这将把取消button后,这是不可取的额外的button。

我相信这是苹果的代码,因为他们的文档上addButtonWithTitle错误:

 // adds a button with the title. returns the index (0 based) of where it was added. buttons are displayed in the order added except for the // destructive and cancel button which will be positioned based on HI requirements. buttons cannot be customized. 

HI要求(苹果自己的人机界面指南)赞成取消button低于所有其他选项,所以我会说苹果搞砸了。 当然,这并不能真正帮助我,所以我又回到了尝试在NSArray和可变参数之间进行转换,我仍然不知道该怎么做。

你可以试试这个

 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]; 

Himanshu的答案的变化:

  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]; 

注意:你的UIActionSheetDelegate方法应该检查button标题而不是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 } } 

我认为人们错过的关键细节是在initWithTitleselect器中设置cancelButton,而不是在所有其他button之后添加它,并通过调用setCancelButtonIndex:来指定取消button的外观。

为什么不做这样的事情:

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

这是我试过的方法似乎工作得很好。 如果您想要在“取消”button之前的button末尾添加一个额外的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]; 

如果你的条件(condition4)没有被满足,在button列表的末尾添加一个额外的'nil'似乎是可以的。

我已经在iOS7上成功testing过了。