如何在动态添加UIActionSheet按钮时进行分组查看

时间:2014-06-23 19:52:51

标签: ios ios7 uiactionsheet

通常情况下,当我按静态向UIActionSheet添加按钮时:

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles: @"Yu", @"Gi", @"Oh", nil];

UIActionSheet的外观会出现如此分组,Cancel按钮会成为一个单独的组:

enter image description here

但是,由于我需要根据用户操作动态添加我的按钮,我发现如果我这样做:

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles: nil];
    [actionSheet addButtonWithTitle:@"Yu"];
    [actionSheet addButtonWithTitle:@"Gi"];
    [actionSheet addButtonWithTitle:@"Oh"]; //This is just an example, could add more or less

结果UIActionSheet如下所示:

enter image description here

我如何能够动态添加按钮,但仍然可以按静态添加按钮进行分组?

谢谢!

1 个答案:

答案 0 :(得分:1)

您需要像其他按钮一样设置“取消”按钮:

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
[actionSheet addButtonWithTitle:@"Yu"];
[actionSheet addButtonWithTitle:@"Gi"];
[actionSheet addButtonWithTitle:@"Oh"]; //This is just an example, could add more or less
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];