【问题标题】:Create UIActionSheet 'otherButtons' by passing in array, not varlist通过传入数组而不是 varlist 创建 UIActionSheet 'otherButtons'
【发布时间】:2011-01-23 22:50:32
【问题描述】:

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

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

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

谢谢。

【问题讨论】:

  • 不确定这是否可行,但 UIActionSheet 的 cancelButtonIndex 属性不是只读的。尝试设置它,看看是否会改变按钮的顺序?

标签: ios iphone uialertview uiactionsheet


【解决方案1】:

有响应的快速版本:

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

【讨论】:

【解决方案2】:

我得到了这个工作(你只需要,对常规按钮没问题,然后在之后添加它:

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

【讨论】:

  • 这行得通;谢谢。没有意识到可以设置cancelButtonIndex 属性。我认为我们都可以同意的一件事:Apple 的 API 很糟糕。
  • 我支持这个!他们 99,99% 的文档也很烂。这就像在不了解该语言的情况下试图从一本用古希伯来语写成的书中学习脑外科手术。
  • 我非常希望我能多次投票。破解答案,我不敢相信我已经走了这么久不知道这一点!
  • @JimThio cancelButton 不是红色的。 destructiveButton 是红色的。
  • 遗憾的是,这个解决方案在 UIActionSheet 中存在一个错误,[UIActionSheet addButtonWithTitle] 似乎没有正确设置firstOtherButtonIndex(应该在代表的actionSheet:clickedButtonAtIndex: 中使用,而不是硬编码 0 或类似的东西) .将NSArray 转换为va_list 听起来很讨厌,这听起来也不是一个很好的解决方案。 :-( 这个 API 太丑了。
【解决方案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];

【讨论】:

    【解决方案4】:

    一个小提示:[actionSheet addButtonWithTitle:] 返回该按钮的索引,所以为了安全和“干净”,您可以这样做:

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

    【讨论】:

    • 这会让取消按钮变成红色吗?
    • @JimThio 不,红色按钮被称为“破坏性按钮”,并且有一个相应的属性destructiveButtonIndex
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    • 1970-01-01
    相关资源
    最近更新 更多