2012-07-10 36 views
7

Tôi mới trong phát triển iPhone,UIAlertView sự kiện Click bên UIAlertView đại biểu

Tôi muốn thực hiện xem 2 cảnh báo cái khác, như khi người dùng nhấn nút xóa, xem cảnh báo 1st sẽ hỏi Are you sure want to Delete ? với hai nút yesno

Bây giờ, nếu người dùng nhấn yes, sau đó xem cảnh báo thứ 2 sẽ đến với thông điệp Deleted Successfully ! xem cảnh báo này chỉ chứa OK nút, bây giờ bấm vào OK nút này tôi muốn gọi một phương pháp.

và nếu người dùng nhấn No thì không có gì xảy ra và cảnh báo nên bỏ qua.

Dưới đây là đoạn mã của tôi,

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

sau khi viết mã này tôi bên trong vòng lặp vô hạn.

Mọi trợ giúp sẽ được đánh giá cao.

+0

Trong trường hợp nếu: 'if (buttonIndex == 0)', tại sao bạn kiểm tra các điều kiện tương tự bên trong nó? –

+0

'if (buttonIndex == 0)' là cho, khi người dùng nhấn nút 'Có' – Krunal

Trả lời

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

Đặt đại biểu quan điểm cảnh báo thứ hai để nil:

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

Hoặc thẻ sử dụng để giải quyết những tình huống như thế sau hoặc đơn giản chỉ cần đặt Đại biểu nil cho alertView bên trong đó là bên trong tole đại biểu để rằng nó sẽ không bao giờ gọi.

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

thử điều này: -

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

      } 
     }