【问题标题】:UIActionSheet addButtonWithTitle: doesn't add buttons in the right orderUIActionSheet addButtonWithTitle:没有按正确的顺序添加按钮
【发布时间】:2009-08-11 18:41:56
【问题描述】:

我已经继承了UIActionSheet,并且在-init 方法中,我必须在调用超级init 之后单独添加按钮(不能传递var_args)。

现在,它看起来像这样:

if (self = [super initWithTitle:title delegate:self cancelButtonTitle:cancel destructiveButtonTile:destroy otherButtonTitles:firstButton,nil]) {
  if (firstButton) {
    id buttonTitle;
    va_list argList;
    va_start(argList, firstButtton);
    while (buttonTitle = va_arg(argList, id)) {
      [self addButtonWithTitle:buttonTitle]
    }
    va_end(argList);
  }
}
return self;

但是,我在这种情况下的具体用途是没有破坏性按钮、一个取消按钮和四个其他按钮。当它出现时,排序全部关闭,显示为

按钮1
取消
按钮2
按钮3

就像它们被简单地添加到列表的末尾一样,这是有道理的;但是,我不希望它看起来像这样;那我该怎么办?事实上,有什么方法可以正确地继承 UIActionSheet 并使其工作?

【问题讨论】:

    标签: iphone cocoa-touch


    【解决方案1】:

    您可以按照正确的顺序添加它们,然后手动设置cancelButtonIndexdestructiveButtonIndex

    对于您的代码示例:

    if (self = [super initWithTitle:title delegate:self cancelButtonTitle:nil destructiveButtonTile:nil otherButtonTitles:nil]) {
      if (firstButton) {
        id buttonTitle;
        int idx = 0;
        va_list argList;
        va_start(argList, firstButtton);
        while (buttonTitle = va_arg(argList, id)) {
          [self addButtonWithTitle:buttonTitle]
          idx++;
        }
        va_end(argList);
        [self addButtonWithTitle:cancel];
        [self addButtonWithTitle:destroy];
        self.cancelButtonIndex = idx++;
        self.destructiveButtonIndex = idx++;
      }
    }
    return self;
    

    【讨论】:

    • 不错的答案,但计数器实际上是不必要的。 addButtonWithTitle:返回它也被添加的索引。
    【解决方案2】:

    Aviad Ben Dov 的回答是正确的,但是不需要按钮索引计数器来设置销毁和取消索引的索引。 addButtonWithTitle: 方法返回新使用按钮的索引,因此我们可以立即使用该值,如下所示:

        if (self = [super initWithTitle:title delegate:self cancelButtonTitle:nil destructiveButtonTile:nil otherButtonTitles:nil]) {
      if (firstButton) {
        id buttonTitle;
        va_list argList;
        va_start(argList, firstButtton);
        while (buttonTitle = va_arg(argList, id)) {
          [self addButtonWithTitle:buttonTitle]
        }
        va_end(argList);
        self.cancelButtonIndex = [self addButtonWithTitle:cancel];
        self.destructiveButtonIndex = [self addButtonWithTitle:destroy];
      }
    }
    return self;
    

    【讨论】:

    • 我认为您的销毁按钮位置不正确。它应该在顶部。
    【解决方案3】:

    前面的回答导致破坏性按钮被放置在底部,不符合HIG,对用户来说也很混乱。破坏性按钮应该在顶部,取消按钮在底部,其他按钮在中间。

    以下命令正确:

    sheetView         = [[UIActionSheet alloc] initWithTitle:title delegate:self
                                           cancelButtonTitle:nil destructiveButtonTitle:destructiveTitle otherButtonTitles:firstOtherTitle, nil];
    if (otherTitlesList) {
        for (NSString *otherTitle; (otherTitle = va_arg(otherTitlesList, id));)
            [sheetView addButtonWithTitle:otherTitle];
        va_end(otherTitlesList);
    }
    if (cancelTitle)
        sheetView.cancelButtonIndex      = [sheetView addButtonWithTitle:cancelTitle];
    

    有关实现(具有基于块的 API 的 UIActionSheet 包装器),请参见 https://github.com/Lyndir/Pearl/blob/master/Pearl-UIKit/PearlSheet.m

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-02
      • 1970-01-01
      • 2014-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多