2010-08-16 11 views
22

Estoy tratando de llamar y alertar cuando se presiona un botón. Yo uso esto:Botón de detección clic con UIAlertView

-(IBAction)Add { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"add button pressed" 
                message:@"Add to record" 
                delegate:nil  
              cancelButtonTitle:@"Cancel" 
              otherButtonTitles:@"OK", nil ]; 
    [alert show]; 
    [alert release];  
} 

bien, no hay problema aquí, aparecieron dos botones, OK y cancelar. Ahora quiero detectar qué botón está presionado yo uso:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    // the user clicked one of the OK/Cancel buttons 
    if (buttonIndex == 0) 
    { 
     //just to show its working, i call another alert view 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"OK WORKIng well" 
                 message:@"no error"                           delegate:nil 
               cancelButtonTitle:@"IWORKS" 
               otherButtonTitles:@"NO PRB", nil]; 
     [alert show]; 
     [alert release];  


    } 
    else 
    { 
     NSLog(@"cancel");  
    } 
} 

ahora aquí está el problema. no puedo detectar qué botón se presiona; la segunda alerta no se muestra. He revisado el código un par de veces, no parece haber ningún problema con él. ningún error/advertencia también.

+2

Mostrar una alerta directamente después de otra alerta probablemente no sea una buena idea, solo un consejo. – vakio

Respuesta

32

Para detectar los clics del botón, la vista de alerta debe tener un delegado asociado, p. Ej.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"add button pressed" 
               message:@"Add to record" 
               delegate:self // <------ 
             cancelButtonTitle:@"Cancel" 
             otherButtonTitles:@"OK", nil]; 
+2

+1: Lo que estaba a la mitad de escribir. También mencionaría que el controlador de vista necesita implementar el 'UIAlertViewDelegate' (aunque creo que da una advertencia si no lo hace) – sdolan

7

El buttonIndex de 0 es el botón cancelar. Yo recomiendo usar:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
if (buttonIndex == 0) 
{ 
     NSLog(@"cancel"); 
} 
else 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"OK works" message:@"no error" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];                            
    [alert show]; 
    [alert release];   
} 
} 
+2

O mejor,' if (buttonIndex == alertView.cancelButtonIndex) ... ' – mojuba

1

Si usted prefiere que su código sea más limpio y sin dependencia de delegado, debe intentar la implementación bloques de UIAlertView:

https://github.com/steipete/PSAlertView

bloques sólo se admiten en iOS 4+ dispositivos sin embargo.

13

Este es su código que utilicé y también agregué mi código. **

-(IBAction) Add 
{ 

      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"add button pressed" 

                 message:@"Add to record" 
                 delegate:nil  
              cancelButtonTitle:@"Cancel" 
              otherButtonTitles:@"OK", nil]; 

     alert.tag=101;//add tag to alert 
     [alert show]; 
     [alert release];  
} 

Ahora, cuando se pulsa el botón en estado de alerta se llamará clickedButtonAtIndex pero debe haber una para cada identificador de alerta. Así que agregue la etiqueta y luego

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

{ 

// the user clicked one of the OK/Cancel buttons 
if(alert.tag=101)  // check alert by tag 
{  

if (buttonIndex == 0) 

    { 

    //just to show its working, i call another alert view 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"OK WORKIng well" 

                message:@"no error" 
                delegate:nil 
              cancelButtonTitle:@"IWORKS" 
              otherButtonTitles:@"NO PRB", nil]; 

    [alert show]; 
    [alert release];  

} 

else 

{ 
    NSLog(@"cancel");  
} 
} 
} 

Espero que ayude.

2

siento si desea mostrar una nueva visión de alerta en el evento de clic de botón de una vista alerta existente, sería mejor utilizar

- (void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 

} 

método delegado en lugar de

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

} 
2
1) 
.h file 
@interface MyClassViewController:<UIAlertViewDelegate> 

2) 
.m file 

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Note" 
               message:@"some message" 
               delegate:self // must be self to call clickedButtonAtIndex 
             cancelButtonTitle:@"Cancel" 
             otherButtonTitles:@"OK", nil]; 


3) 
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 

    if (buttonIndex == [alertView cancelButtonIndex]) { 
    NSLog(@"The cancel button was clicked from alertView"); 
    } 
else { 

} 
} 
Cuestiones relacionadas