通过传入数组而不是varlist来创建UIActionSheet的"otherButtons"

Gre*_*tic 110 iphone uialertview uiactionsheet ios

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

那我怎么能把这些标题传递到UIActionSheet呢?我见过的解决方法是将nil传递给otherButtonTitles :,然后使用addButtonWithTitle:单独指定按钮标题.但这有将"取消"按钮移动到UIActionSheet上的第一个位置而不是最后一个位置的问题; 我希望它是最后一个.

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

谢谢.

Jab*_*Jab 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];
Run Code Online (Sandbox Code Playgroud)

  • 这有效; 谢谢.没意识到可以设置cancelButtonIndex属性.有一件事我认为我们都可以同意:Apple的API很糟糕. (19认同)
  • 我是第二个!其99,99%的文档也很糟糕.这就像试图从古老的希伯来语书中学习脑部手术而不知道那种语言. (11认同)
  • 我非常希望能够多次投票.破解答案,我不敢相信我已经走了这么久不知道这个! (4认同)

Nic*_*ick 78

一个小注意事项:[actionSheet addButtonWithTitle:]返回该按钮的索引,因此为了安全起见并"干净",您可以这样做:

actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];
Run Code Online (Sandbox Code Playgroud)