2012-07-10 18 views
7

Soy nuevo en el iPhone desarrollador,UIAlertView evento Click en el interior UIAlertView delegado

Quiero aplicar 2 Alerta vista, uno tras otro, como cuando el usuario pulse el botón borrar, primera vista de alertas le preguntará Are you sure want to Delete ? con dos botones yes y no

ahora, si el usuario presiona yes, entonces segunda vista de alertas vendrá con el mensaje de alerta Deleted Successfully ! este punto de vista sólo contiene OK botón, ahora al hacer clic con el botón OK Quiero llamar a un método.

y si el usuario presiona No, entonces no debería pasar nada y la alerta debería descartarse.

Aquí es mi fragmento de código,

-(void)DeletebtnCliked:(id)sender 
{ 
    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Are you sure want to delete ?" 
                 message:nil delegate:self 
               cancelButtonTitle:nil 
               otherButtonTitles:@"Yes",@"No",nil]; 
    [alertView show]; 
    [alertView release]; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 0) 
    {    
     UIAlertView* alertew = [[UIAlertView alloc] initWithTitle:@"Deleted Successfully !" 
                  message:nil delegate:self 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
     [alertew show]; 
     [alertew release]; 

     if (buttonIndex == 0) 
     { 
      [self MethodCall]; 
     } 
    } 
    else if (buttonIndex == 1) 
    { 
     [alertView dismissWithClickedButtonIndex:1 animated:TRUE]; 
    } 
} 

después de escribir el código que estoy en el interior del bucle infinito.

Cualquier ayuda será apreciada.

+0

En el caso si: 'Si (buttonIndex == 0)', ¿por qué estás mirando por la misma afección dentro de ella? –

+0

'if (buttonIndex == 0)' es para cuando el usuario presiona el botón 'Sí' – Krunal

Respuesta

11
alertView.tag = 1; 
alertew.tag = 2; 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (alertView.tag == 2) 
    { 
     //Do something 
    } 
    else 
    { 
     //Do something else 
    } 
} 
4

Establecer delegado de la segunda de vista de alertas a cero:

UIAlertView* alertew = [[UIAlertView alloc] initWithTitle:@"Deleted Successfully !" 
                  message:nil delegate:nil 
                cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
1

Cualquiera de las etiquetas de uso para hacer frente a la situación como seguir o simplemente establecer nula Delegado para la alertView interior que está dentro de la méthode delegado así lo que nunca llamará

-(void)DeletebtnCliked:(id)sender 
{ 
    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Are you sure want to delete ?" 
                message:nil delegate:self 
              cancelButtonTitle:nil 
              otherButtonTitles:@"Yes",@"No",nil]; 
alertView.tag = 1; 
[alertView show]; 
[alertView release]; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
if (buttonIndex == 0 && alertView.tag == 1) 
{    
    UIAlertView* innerAlert = [[UIAlertView alloc] initWithTitle:@"Deleted Successfully !" 
                 message:nil delegate:nil 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 
    innerAlert.tag = 2; 
    [innerAlert show]; 
    [innerAlert release]; 

    if (buttonIndex == 0 && alertView.tag == 1) 
    { 
     [self MethodCall]; 
    } 
} 
else if (buttonIndex == 1 && alertView.tag == 1) 
{ 
    [alertView dismissWithClickedButtonIndex:1 animated:TRUE]; 
} 
} 
0

probar esto: -

-(void)button:(UIButton *)buttonDelete{ 
      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"delete" message:@"Do you Want to delete" delegate:self cancelButtonTitle:@"Cancle" otherButtonTitles:@"Yes", nil]; 
      alertView.delegate = self; 
      alertView.tag = 2000; 
      [alertView show]; 
     } 
-(void)buttonUpdate:(UIButton *)buttonEdit{ 

     UIAlertView *alertView2 = [[UIAlertView alloc] initWithTitle:@"Update" message:@"Do you Want to Update" delegate:self cancelButtonTitle:@"Cancle" otherButtonTitles:@"Yes", nil]; 
      alertView2.delegate = self; 
      alertView2.tag = 20001; 
      [alertView show]; 
    } 
-(void)buttonAdd:(UIButton *)buttonAdd{ 
     UIAlertView *alertView3 = [[UIAlertView alloc] initWithTitle:@"Add" message:@"Do you Want to Add" delegate:self cancelButtonTitle:@"Cancle" otherButtonTitles:@"Yes", nil]; 
      alertView3.delegate = self; 
      alertView3.tag = 20002; 
      [alertView show]; 
    }  
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
      if (alertView.tag == 2000){ 
       if (buttonIndex==0) { 
        NSLog(@"Delete Cancel Button click"); 
       } 
       else{ 
        NSLog(@"Delete yes Button is click"); 
       } 

      } 
      if (alertView.tag == 20001){ 
       if (buttonIndex==0) { 
        NSLog(@"update Cancel Button click"); 
       } 
       else{ 
        NSLog(@"update yes Button is click"); 
       } 

      } 
      if (alertView.tag == 20002){ 
       if (buttonIndex==0) { 
        NSLog(@"Add Cancel Button click"); 
       } 
       else{ 
        NSLog(@"Add yes Button is click"); 
       } 

      } 
     }