2011-07-06 9 views
5

Creé dos hojas de acción en una vista. Hay dos botones, cada uno iniciará una hoja de acción.Creación de dos hojas de acción en una vista

El problema: cuando presiono la primera opción en ambas hojas de acción, se desencadena la misma acción.

Aquí está mi código:

-(IBAction) ChangeArrow:(id)sender{ 
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Change Arrow" 
                 delegate:self 
               cancelButtonTitle:@"cancel" 
              destructiveButtonTitle:@"Red" 
               otherButtonTitles:@"Blue",@"Black",nil]; 
[actionSheet showInView:self.view]; 
[actionSheet release];} 
- (void) actionSheet: (UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{ 
if (buttonIndex ==[actionSheet destructiveButtonIndex]) { 
    self.bar.image=[UIImage imageNamed:@"red"]; 

} 
else if(buttonIndex == 1){ 
    self.bar.image=[UIImage imageNamed:@"blue"]; 

} 
else if(buttonIndex == 2){ 
    self.bar.image=[UIImage imageNamed:@"dark"];} 
} 

// Segunda Hoja de acción:

-(IBAction) Background:(id)sender{ 
UIActionSheet *actionSheet2 = [[UIActionSheet alloc] initWithTitle:@"Change Background" 
                 delegate:self 
               cancelButtonTitle:@"cancel" 
              destructiveButtonTitle:@"Sky" 
               otherButtonTitles:@"Thumbs",@"Smiley",nil]; 
[actionSheet2 showInView:self.view]; 
[actionSheet2 release]; 
} 
- (void) actionSheet2: (UIActionSheet *)actionSheet2 didDismissWithButtonIndex:(NSInteger)buttonIndex { 
if (buttonIndex ==[actionSheet2 destructiveButtonIndex]) { 
    self.background.image=[UIImage imageNamed:@"sky"]; 

} 
else if(buttonIndex == 1){ 
    self.background.image=[UIImage imageNamed:@"thumbs"]; 

} 
else if(buttonIndex == 2){ 
    self.background.image=[UIImage imageNamed:@"smiley"];} 
} 
+0

Daniel Dickison thnx, todavía estoy teniendo algunos problemas al escribir un código: P –

Respuesta

25

Establecer la propiedad etiqueta en cada actionsheet a un valor diferente. Luego puede consultar sender.tag para ver qué hoja de acción llamó a su método.

Ej.

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Change Arrow" 
                delegate:self 
              cancelButtonTitle:@"cancel" 
             destructiveButtonTitle:@"Red" 
              otherButtonTitles:@"Blue",@"Black",nil]; 
actionSheet.tag = 1; 
UIActionSheet *actionSheet2 = [[UIActionSheet alloc] initWithTitle:@"Change Arrow" 
                 delegate:self 
               cancelButtonTitle:@"cancel" 
              destructiveButtonTitle:@"Red" 
               otherButtonTitles:@"Blue",@"Black",nil]; 
actionSheet2.tag = 2; 

Entonces

- (void) actionSheet: (UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{ 
if(actionSheet.tag == 1) { 
    //do something 
} else if(actionSheet.tag == 2) { 
    //do something else 
} 
} 
+0

añadir a la respuesta correcta de David, comprobar para ver cuál de los remitentes que estaba en el delegado método. –

+0

y cómo se puede hacer esto? –

+0

+1 a David: vea el ejemplo editado en la respuesta de David para la mejor/más fácil manera –

Cuestiones relacionadas