如何向现有UIActionSheet添加按钮?

时间:2010-03-11 02:42:57

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

并且可以有条件地添加我的LMNOP按钮。

...取消按钮后。

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

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

因为那肯定会有所帮助。

有什么想法吗?

谢谢!

2 个答案:

答案 0 :(得分: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"];

答案 1 :(得分:-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];