2010-03-11 50 views
20

我有这样的代码:如何将按钮添加到现有的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. 

因为这肯定会有所帮助。

任何想法?

谢谢!

回答

53

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

​​
+3

啊哈!我错过了sheet.cancelButtonIndex = ...部分;这就是我需要完成的图片。 谢谢! – Olie 2010-03-12 15:11:29

+0

也可以使用'sheet.destructiveButtonIndex'。 – zekel 2014-06-25 16:37:22

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