2010-11-14 23 views
5

Estoy seguro de que esto debe ser respondido en algún lugar ya, pero estoy luchando para encontrar los términos de búsqueda correctos para la respuesta.¿Cómo pasar elementos de una matriz a la función variadic?

En mi código object-c tengo un NSArray de un número desconocido de cadenas que quiero pasar sus elementos a un método init variódico, en este caso es la ... lista de 'otherButtonTitles' en el constructor de UIActionSheet . ¿Cómo se puede lograr esto?

gracias de antemano

Respuesta

3

creo que tendría que pasar el primer elemento del array al constructor y luego usar el método addButtonWithTitle colocar a través de los elementos restantes y añadirlos:

UIActionSheet *mySheet = [[UIActionSheet alloc] initWithTitle:title delegate:delegate cancelButtonTitle:cancelButtonTitle destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:[myOtherButtons objectAtIndex:0],nil]; 

NSMutableArray *otherbuttons = myOtherButtons; 
[otherButtons removeObjectAtIndex:0]; 

NSEnumerator *enumerator = [otherButtons objectEnumerator]; 
id anObject; 

while (title = [enumerator nextObject]) { 
    [mySheet addButtonWithTitle:title]; 
} 
+0

Supongo que esperaba algo un poco más elegante, gracias, sin embargo, funciona bien. – Elric

3

No hay una manera general de hacerlo, pero específicamente para UIActionSheet, no necesita usar ese constructor.

UIActionSheet *sheet = [[UIActionSheet alloc] init]; 

// set properties 
sheet.title = @"Title!"; 

// add buttons 
for (NSString *buttonTitle in otherButtonTitles) { 
    [sheet addButtonWithTitle:buttonTitle]; 
} 

sheet.cancelButtonIndex = 
    [sheet addButtonWithTitle:@"Cancel"]; 

UIAlertView se puede inicializar de forma similar.

Cuestiones relacionadas