如何向现有UIActionSheet添加按钮?

Oli*_*lie 20 dynamic button uiactionsheet

我有这个代码:

UIActionSheet *actionSheet = [[[UIActionSheet alloc]
                initWithTitle:@"Illustrations"
                delegate:self
                cancelButtonTitle:@"Cancel"
                destructiveButtonTitle:nil
                otherButtonTitles: @"ABC", @"XYZ",
                nil] autorelease];
UIImage *image = // whatever, snip
if (image != nil)
{
    [actionSheet addButtonWithTitle:@"LMNOP"];
}
Run Code Online (Sandbox Code Playgroud)

它可以有条件地添加我的LMNOP按钮.

...取消按钮后.

如何使用条件按钮构建我的操作表?可悲的是,我做不到:

UIActionSheet *actionSheet = [[[UIActionSheet alloc]
      // ... etc.
      otherButtonTitles: someMutableArray
      // ... etc.
Run Code Online (Sandbox Code Playgroud)

因为那肯定会有所帮助.

有任何想法吗?

谢谢!

ken*_*ytm 53

您可以在init方法之后添加所有按钮.

UIActionSheet* sheet = [[[UIActionSheet alloc] init] autorelease];
sheet.title = @"Illustrations";
sheet.delegate = self;
[sheet addButtonWithTitle:@"ABC"];
[sheet addButtonWithTitle:@"XYZ"];
if (condition)
    [sheet addButtonWithTitle:@"LMNOP"];
sheet.cancelButtonIndex = [sheet addButtonWithTitle:@"Cancel"];
Run Code Online (Sandbox Code Playgroud)

  • 啊哈!我错过了sheet.cancelButtonIndex = ... part; 这就是我完成图片所需要的.谢谢! (3认同)