2010-03-11 10 views
20

tengo este código:¿Cómo agregar un botón a una hoja de UIActionSheet existente?

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

y se hace un gran trabajo de añadir el botón de mi LMNOP condicionalmente.

... DESPUÉS del botón cancelar.

¿Cómo puedo construir mi hoja de acción con un botón condicional? Lamentablemente, no puedo hacer:

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

porque eso sin duda ayudaría.

¿Alguna idea?

Gracias!

Respuesta

53

Puede agregar todos los botones después del método 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! Me había perdido la hoja.cancelButtonIndex = ... parte; eso es lo que necesitaba para completar la imagen. Gracias! – Olie

+0

También funciona con 'sheet.destructiveButtonIndex'. – zekel

-4

estoy programando en iOS 4 y este es el método que se utiliza. Simplemente agrega el título que desea para el botón en las otras secciones de botones.

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