如何向现有的UIActionSheet添加按钮?

20

我有这段代码:

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"];
}

它非常出色地实现了有条件地添加我的LMNOP按钮。

...在取消按钮之后。

如何构建带有条件按钮的操作表?可惜,我不能这样做:

UIActionSheet *actionSheet = [[[UIActionSheet alloc]
      // ... etc.
      otherButtonTitles: someMutableArray
      // ... etc.

因为这肯定会有帮助。

有什么想法吗?

谢谢!

2个回答

51
你可以在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"];

3
啊哈!我错过了 sheet.cancelButtonIndex = ... 部分;那就是我需要完成这个画面所需的东西。谢谢! - Olie

-4

我正在编写iOS 4的代码,以下是使用的方法。您只需在其它按钮部分添加所需的按钮标题即可。

UIActionSheet *phoneActionSheet = [[UIActionSheet alloc]
                                          initWithTitle:@"Do you want to call or text this person?"
                                          delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          destructiveButtonTitle:@"Call"                                            
                                          otherButtonTitles:@"Text",nil];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接