1

我以这种方式在 iPad 上显示 UIActionSheet:

_actionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose action"
                                           delegate:(id<UIActionSheetDelegate>)self
                                  cancelButtonTitle:nil
                             destructiveButtonTitle:nil
                                  otherButtonTitles:nil];

if (special_case) {
    _actionSheet.destructiveButtonIndex = [_actionSheet addButtonWithTitle:@"Discard Action"];
}

[_actionSheet addButtonWithTitle:@"Action One"];
[_actionSheet addButtonWithTitle:@"Action Two"];
_actionSheet.cancelButtonIndex = [_actionSheet addButtonWithTitle:@"Cancel"];

[_actionSheet showFromBarButtonItem:self.myActionButton animated:YES];

虽然两者都cancelButtonIndex可以destructiveButtonIndex正常工作,但奇怪的是在 actionSheet:didDismissWithButtonIndex: 中我firstOtherButtonIndex设置为-1,这显然是错误的,因为我的Action One按钮的索引为1,而Action Two索引为2.

显然,上面的重点是仅在特殊情况下才显示破坏性按钮(否则我会在 init 调用中传递所有标题,并且可能事情会很甜蜜)。

我错过了什么?

4

1 回答 1

2

firstOtherButtonIndex可能只在initWithTitle方法中设置。如果您手动添加按钮,则必须手动设置。该问题也可能是由于您在添加其他按钮后添加了取消按钮。通常破坏性和取消按钮的索引低于firstOtherButtonIndex.

用简单的方法怎么办?


_actionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose action"
                                           delegate:(id)self
                                  cancelButtonTitle:@"Cancel"
                             destructiveButtonTitle:(special_case ? @"Discard" : nil)
                                  otherButtonTitles:@"Action One", @"Action Two", nil];
于 2012-07-23T08:35:25.957 回答