通过传递数组创建UIActionSheet的“otherButtons”,而不是通过varlist。

110

我有一个字符串数组,想将其用作UIActionSheet上按钮的标题。不幸的是,方法调用中的otherButtonTitles:参数需要一个可变长度的字符串列表,而不是一个数组。

那么我如何将这些标题传递到UIActionSheet中呢?我看到的解决方法是在otherButtonTitles:中传递nil,然后使用addButtonWithTitle:单独指定按钮标题。但是这会将“取消”按钮移到UIActionSheet的第一个位置,而不是最后一个位置;我希望它在最后一个位置。

是否有办法:1)传递一个数组代替可变的字符串列表,或者2)将取消按钮移动到UIActionSheet的底部?

谢谢。


不确定这是否有效,但是UIActionSheet的cancelButtonIndex属性不是只读的。尝试设置它并查看是否更改了按钮的顺序? - lostInTransit
4个回答

248
我让这个起作用了(你只需要使用一个普通按钮,并将其添加在“我让这个起作用了(你只需要使用一个普通按钮,并将其添加在“</”后即可。)

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

19
可以,谢谢。我没有意识到可以设置 cancelButtonIndex 属性。有一件事我想我们都可以认同:苹果的 API 真的很烂。 - Greg Maletic
11
我同意!他们99.99%的文档也很糟糕。就像要从一本用古代希伯来语写的书中学习脑外科手术一样,而你又不懂那种语言。 - Duck
4
我非常希望我能给这个答案投更多的赞。太好了,我简直不敢相信我这么久都不知道这个! - mattjgalloway
1
@JimThio cancelButton 不是红色的。destructiveButton 是红色的。 - Armin
1
不幸的是,这个解决方案存在一个 UIActionSheet 中的 bug,即 [UIActionSheet addButtonWithTitle] 似乎没有正确设置 firstOtherButtonIndex(应该在委托的 actionSheet:clickedButtonAtIndex: 中使用而不是硬编码为0或类似的东西)。将 NSArray 转换为 va_list 并不是一个好的解决方案。 :-( 这个 API 真的很丑陋。 - Daniel Rinser
显示剩余3条评论

78

需要注意的是:[actionSheet addButtonWithTitle:] 返回那个按钮的索引,因此为了安全和“干净”,可以这样做:

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

那会把取消按钮变成红色吗? - user4951
1
@JimThio 不是,红色按钮被称为“destructiveButton”,并且有相应的属性destructiveButtonIndex - Nick

3
采纳Jaba和Nick的答案并进一步扩展。为了将销毁按钮纳入此解决方案:
// 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];

2

这里是响应的Swift版本:

//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]
}

对于未来的用户...它可以工作...但已被弃用...请使用https://dev59.com/el4c5IYBdhLWcg3wssFs#41322537 - jeet.chanchawat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接