通过传递数组而不是varlist来创buildUIActionSheet'otherButtons'

我有一个string数组,我想在UIActionSheet上使用button标题。 不幸的是,方法调用中的otherButtonTitles:参数需要一个可变长度的string列表,而不是一个数组。

那么我怎样才能将这些标题传递给UIActionSheet? 我见过的解决方法是将nil传递给otherButtonTitles,然后使用addButtonWithTitle:来分别指定button标题。 但是,这有一个问题,将“取消”button移到UIActionSheet的第一个位置,而不是最后一个; 我希望它是最后一个。

有没有办法1)传递数组来代替string的variables列表,或者2)将取消button移动到UIActionSheet的底部?

谢谢。

我得到了这个工作(你只需要,一个正常的button,只需添加它:

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

有一点需要注意:[actionSheet addButtonWithTitle:]返回该button的索引,所以为了安全和“干净”,你可以这样做:

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

以贾巴和尼克的答案,并进一步扩大他们。 将破坏button合并到此解决scheme中:

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

有响应的迅速版本:

 //array with button titles private var values = ["Value 1", "Value 2", "Value 3"] //create action sheet let actionSheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: nil, destructiveButtonTitle: nil) //for each value in array for value in values{ //add a button actionSheet.addButtonWithTitle(value as String) } //display action sheet actionSheet.showInView(self.view) 

要获取所选值,请将委托添加到您的ViewController中:

 class MyViewController: UIViewController, UIActionSheetDelegate 

并执行方法“clickedButtonAtIndex”

 func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) { let selectedValue : String = values[buttonIndex] }