2012-07-10 22 views
7

Sono nuovo nel sviluppatore iPhone,UIAlertView evento Click all'interno UIAlertView delegato

Voglio realizzare vista 2 Allarme uno dopo l'altro, come quando l'utente preme il tasto di cancellazione, prima vista avviso chiederà Are you sure want to Delete ? con due pulsanti yes e no

ora, se l'utente preme yes, quindi 2nd view avviso sarà disponibile con un messaggio Deleted Successfully ! questo punto di vista avviso contiene solo OK tasto, d'ora in poi click di questo pulsante OK voglio chiamare un metodo.

e se l'utente preme No allora non dovrebbe accadere nulla e l'avviso dovrebbe essere ignorato.

Ecco il mio frammento di codice,

-(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]; 
    } 
} 

dopo aver scritto questo codice sono dentro ciclo infinito.

Qualsiasi aiuto sarà apprezzato.

+0

Nel caso se: 'se (buttonIndex == 0)', perché stai controllando per la stessa condizione al suo interno? –

+0

'if (buttonIndex == 0)' è per, quando l'utente preme il tasto 'Si' – Krunal

risposta

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

Impostare delegato della seconda visualizzazione degli avvisi a zero:

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

usare sia i tag per affrontare la situazione come segue o semplicemente impostare a zero Delegato per l'alertView interiore che si trova all'interno del methode delegato in modo che non chiamerà mai.

-(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

provare questo: -

-(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"); 
       } 

      } 
     }