iphone - UIActionSheet addButtonWithTitle : doesn't add buttons in the right order

标签 iphone cocoa-touch

我已经子类化了 UIActionSheet,并且在 -init 方法中,我必须在调用 super 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 并使其工作?

最佳答案

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

对于您的代码示例:

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;

关于iphone - UIActionSheet addButtonWithTitle : doesn't add buttons in the right order,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1262205/

相关文章:

iphone - 如何以编程方式截取 iPhone 的屏幕截图?

iphone - 如何手动读取JSON

iPhone 后台任务在某个时刻停止运行

iphone - 如何制作像SpeedTask App那样的UI?

ios - 自定义默认当前位置提醒消息 (iOS)

ios - UIDatePicker 显示不正确

ios - 如果在 tableView :cellForRowAtIndexPath 设置 cell.backgroundColor 不响应

iphone - 适用于 iOS 4 应用程序的 Xcode 4.2 模板

ios - 如何使用扩展设置 viewController 搜索栏的委托(delegate)?

iphone - 旋转时重绘 UITableViewCells