2010-03-11 33 views
20

tôi có mã này:Cách thêm nút vào UIActionSheet hiện có?

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

và nó làm một công việc tuyệt vời của việc thêm nút LMNOP tôi có điều kiện.

... SAU nút hủy.

Tôi làm cách nào để xây dựng trang tính của mình bằng nút có điều kiện? Đáng buồn thay, tôi không thể làm:

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

vì điều đó chắc chắn sẽ hữu ích.

Bất kỳ ý tưởng nào?

Cảm ơn!

Trả lời

53

Bạn có thể thêm tất cả các nút sau phương thức 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

Aha! Tôi đã bỏ lỡ sheet.cancelButtonIndex = ... part; đó là những gì tôi cần để hoàn thành bức tranh. Cảm ơn! – Olie

+0

Cũng hoạt động với 'sheet.destructiveButtonIndex'. – zekel

-4

im mã hóa cho iOS 4 và đây là phương pháp được sử dụng. Bạn chỉ cần thêm tiêu đề bạn muốn cho nút trong phần otherbutton.

UIActionSheet *phoneActionSheet = [[UIActionSheet alloc] 
              initWithTitle:@"Do you want to call or text this person?" 
              delegate:self 
              cancelButtonTitle:@"Cancel" 
              destructiveButtonTitle:@"Call"            
              otherButtonTitles:@"Text",nil]; 
Các vấn đề liên quan