在uiactionsheet上设置“取消”按钮会导致最后一项消失

时间:2014-05-22 02:53:41

标签: objective-c uiactionsheet dynamically-generated cancel-button

我正在动态地向我的uiactionsheet添加按钮。我最后添加了一个“取消”按钮,但如果我使用方法setCancelButtonIndex或手动设置索引,则“取消”不会显示。如果我排除了setCancelButtonIndex调用,那么就有取消(当然没有突出显示)。我错过了一个脚注吗?

    UIActionSheet *actionSheet;
actionSheet = [[UIActionSheet alloc] initWithTitle:@"What do you want to do?" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
for (NSString *title in arrayButtons) {
    [actionSheet addButtonWithTitle:title];
}
[actionSheet addButtonWithTitle:@"Cancel"];
[actionSheet setCancelButtonIndex:[arrayButtons count]-1];
[actionSheet setDestructiveButtonIndex:0];
[actionSheet showInView:self.view];

1 个答案:

答案 0 :(得分:1)

方法-addButtonWithTitle:返回按钮索引。所以你应该这样做:

UIActionSheet *actionSheet = [[UIActionSheet alloc] init];
[actionSheet setTitle:@"What do you want to do?"];
[actionSheet setDelegate:self];
for (NSString *title in arrayButtons) {
    [actionSheet addButtonWithTitle:title];
}
[actionSheet setCancelButtonIndex:[actionSheet addButtonWithTitle:@"Cancel"]];     
[actionSheet setDestructiveButtonIndex:0];
[actionSheet showInView:self.view];