2012-04-02 18 views
6

chicos:UIAlertViewDelegate: clickedButtonAtIndex y dos botones

Hay dos botones en mi viewController de aplicación de prueba, la correcta llamo "NO",

y el otro es "sí". Los dos botones llamarán a dos funciones diferentes, y cuando

usuario presionen uno de los botones, quiero mostrar al usuario una alerta para confirmarlo.

sé usar el UIAlertViewDelegate

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 

pero hay dos botones, estoy confundido. ¿Cómo puedo saber qué botón se presiona?

Por lo tanto, los pls me ayudan con esto, gracias de antemano!

Respuesta

17

Cuando se crea un UIAlertView se puede configurar un tag para él

-(IBAction)yesButtonClick:(id)sender{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle: @"Cancel" otherButtonTitles:@"OK", nil]; 
    alert.tag = 101; 
    [alert show]; 
} 

-(IBAction)noButtonClick:(id)sender{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle: @"Cancel" otherButtonTitles:@"OK", nil]; 
    alert.tag = 102; 
    [alert show]; 
} 

En el método de verificación delegado cual se muestra una alerta

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (alertView.tag == 101) { 
     // from YES button 
    } 
    else if (alertView.tag == 102) { 
     // from NO button 
    } 
} 
+0

¡Gracias! Eso funciona ! Tenga un buen día ! – jxdwinter

0
- (void)alertView:(UIAlertView *)actionSheet 
    clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    switch(buttonIndex){ 
    case 0: 
     //YES button handler 
     break; 
    case 1: 
     //NO button handler 
     break; 
    default: 
     break; 
    } 
} 
+0

supongo que quiere identificar qué botón de muestra esta alerta .. – beryllium

+0

@beryllium: si incluso me piensa lo mismo –

+0

Sí, me gustaría saber es el botón "SÍ" o el botón "NO" presionado, y luego asegúrese de que el usuario quiera continuar, ¡gracias por sus respuestas! – jxdwinter

0

puede utilizar el atributo de etiqueta para hacer la diferencia entre su remolque UIAlertView
en la función del botón 1
alertView1.tag=1;
y en

-(void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
if(actionSheet.tag==1){ 
//first button was clicked 
} 

} 
Cuestiones relacionadas