2010-03-11 16 views
20

Ho questo codice:Come aggiungere un pulsante a un UIActionSheet esistente?

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

e fa un grande lavoro di aggiungere condizionalmente mio tasto LMNOP.

... DOPO il pulsante Annulla.

Come posso costruire il mio foglio di azione con un pulsante condizionale? Purtroppo, non posso fare:

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

perché questo sarebbe certamente di aiuto.

Qualche idea?

Grazie!

risposta

53

È possibile aggiungere tutti i pulsanti dopo il metodo 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! Mi ero perso il foglio.cancelButtonIndex = ... parte; questo è quello di cui avevo bisogno per completare l'immagine. Grazie! – Olie

+0

Funziona anche con 'sheet.destructiveButtonIndex'. – zekel

-4

im codifica a IOS 4 e questo è il metodo che viene utilizzato. Basta aggiungere il titolo desiderato per il pulsante nelle sezioni otherbutton.

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