【问题标题】:Use NSArray to specify otherButtonTitles?使用 NSArray 指定 otherButtonTitles?
【发布时间】:2010-12-08 19:14:59
【问题描述】:

UIAlertSheet 的构造函数将 otherButtonTitles 参数作为 varg 列表。我想从 NSArray 指定其他按钮标题。这可能吗?

即我必须这样做:

id alert = [[UIActionSheet alloc] initWithTitle: titleString
                                  delegate: self
                                  cancelButtonTitle: cancelString
                                  destructiveButtonTitle: nil
                                  otherButtonTitles: button1Title, button2Title, nil];

但由于我是在运行时生成可用按钮列表,所以我真的想要这样的东西:

id alert = [[UIActionSheet alloc] initWithTitle: titleString
                                       delegate: self
                              cancelButtonTitle: cancelString
                         destructiveButtonTitle: nil
                              otherButtonTitles: otherButtonTitles];

现在,我想我需要单独致电initWithTitle: 获取 1 件、2 件和 3 件。像这样:

if ( [titles count] == 1 ) {
     alert = [[UIActionSheet alloc] initWithTitle: titleString
                                         delegate: self
                                cancelButtonTitle: cancelString
                           destructiveButtonTitle: nil
                                otherButtonTitles: [titles objectAtIndex: 0], nil];
} else if ( [titles count] == 2) {
     alert = [[UIActionSheet alloc] initWithTitle: titleString
                                         delegate: self
                                cancelButtonTitle: cancelString
                           destructiveButtonTitle: nil
                                otherButtonTitles: [titles objectAtIndex: 0], [titles objectAtIndex: 1],  nil];
} else {
    // and so on
}

这是很多重复的代码,但实际上它可能是合理的,因为我最多有三个按钮。我怎样才能避免这种情况?

【问题讨论】:

    标签: iphone nsarray uialertsheet


    【解决方案1】:

    这是一岁了,但解决方案非常简单......按照@Simon的建议做,但不要指定取消按钮标题,所以:

    UIActionSheet *alert = [[UIActionSheet alloc] initWithTitle: titleString
                                  delegate: self
                                  cancelButtonTitle: nil
                                  destructiveButtonTitle: nil
                                  otherButtonTitles: nil];
    

    但是在添加普通按钮后,添加取消按钮,例如:

    for( NSString *title in titles)  {
        [alert addButtonWithTitle:title]; 
    }
    
    [alert addButtonWithTitle:cancelString];
    

    现在关键步骤是指定哪个按钮是取消按钮,比如:

    alert.cancelButtonIndex = [titles count];
    

    我们使用[titles count] 而不是[titles count] - 1,因为我们从titles 的按钮列表中额外添加了取消按钮。

    您现在还可以通过指定破坏性按钮索引(通常是[titles count] - 1 按钮)来指定您希望哪个按钮成为破坏性按钮(即红色按钮)。此外,如果您将取消按钮保留为最后一个按钮,iOS 将在其他按钮和取消按钮之间添加很好的间距。

    所有这些都兼容 iOS 2.0,所以尽情享受吧。

    【讨论】:

    • 不知道为什么它对你有用,但我必须执行“[titles count] - 1”才能让它对我有用,iOS 7。
    • [alert numberOfButtons]-1 是另一种设置取消按钮索引的方法
    • 小注:使用您在这里建立的名称,我认为sheet.cancelButtonIndex 应该是alert.cancelButtonIndex,是吗?
    • @Matt 是的,你是对的。我已经相应地编辑了答案。
    【解决方案2】:

    不要在初始化 UIActionSheet 时添加按钮,而是尝试使用 addButtonWithTitle 方法使用通过 NSArray 的 for 循环添加它们。

    UIActionSheet *alert = [[UIActionSheet alloc] initWithTitle: titleString
                                  delegate: self
                                  cancelButtonTitle: cancelString
                                  destructiveButtonTitle: nil
                                  otherButtonTitles: nil];
    
    for( NSString *title in titles)  
        [alert addButtonWithTitle:title]; 
    

    【讨论】:

    • 比这更简单:for (NSString * title in titles) { [alert addButtonWithTitle:title]; } 无需创建枚举器。
    • 这使得按钮出现在取消按钮之后。有什么办法可以解决这个@bent 或@simon? (请参阅stackoverflow.com/questions/7781022 上的编辑)
    • 尝试使用cancelButtonIndex 属性。
    • @MicahHainline - 只需在“for”循环的末尾添加这一行:[actionSheet addButtonWithTitle:@"Cancel_Button_Title"]; actionSheet.cancelButtonIndex = actionSheet.numberOfButtons-1;
    • 这会为我添加取消按钮下方的按钮。
    【解决方案3】:

    addButtonWithTitle:返回添加按钮的索引。在 init 方法中将 cancelButtonTitle 设置为 nil 并在添加其他按钮后运行:

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

    【讨论】:

      【解决方案4】:
      - (void)showActionSheetWithButtons:(NSArray *)buttons withTitle:(NSString *)title {
      
          UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle: title 
                                                                   delegate: self
                                                          cancelButtonTitle: nil 
                                                     destructiveButtonTitle: nil 
                                                          otherButtonTitles: nil];
      
          for (NSString *title in buttons) {
              [actionSheet addButtonWithTitle: title];
          }
      
          [actionSheet addButtonWithTitle: @"Cancel"];
          [actionSheet setCancelButtonIndex: [buttons count]];
          [actionSheet showInView:self.view];
      }
      

      【讨论】:

        【解决方案5】:

        您可以添加取消按钮并设置如下:

        [actionSheet setCancelButtonIndex: [actionSheet addButtonWithTitle: @"Cancel"]];
        

        【讨论】:

          【解决方案6】:

          我知道这是一篇旧帖子,但以防其他人(如我)试图解决这个问题。

          (@kokemomuke 回答了这个问题。这主要是更详细的解释。同样基于 @Ephraim 和 @Simon)

          原来 addButtonWithTitle: 的 LAST 条目需要是Cancel 按钮。我会使用:

          // All titles EXCLUDING Cancel button
          for( NSString *title in titles)  
              [sheet addButtonWithTitle:title];
          
          
          // The next two line MUST be set correctly: 
          // 1. Cancel button must be added as the last entry
          // 2. Index of the Cancel button must be set to the last entry
          
          [sheet addButtonWithTitle:@"Cancel"];
          
          sheet.cancelButtonIndex = titles.count - 1;
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-04-10
            • 2011-04-28
            • 2012-12-02
            • 1970-01-01
            • 2013-01-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多