2012-07-10 103 views
7

我在iPhone开发人员是新的,UIAlertView中Click事件

我要陆续实行2警报视图中的一个,就像当用户按下删除键,第一个警报视图会问Are you sure want to Delete ?两个按钮yesno现在

,如果用户按下yes,然后第二个警报视图将拿出消息Deleted Successfully !此警报视图仅包含OK按钮,现在这个OK按钮,我想打电话给一个方法的点击。

如果用户按No则不会发生任何事情,警报应该解雇。

这里是我的代码片段,

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

编写这些代码我是无限循环中之后。

任何帮助将不胜感激。

+0

在若情况:'如果(buttonIndex == 0)',你为什么要为它里面的相同条件检查? – 2012-07-10 08:50:06

+0

'if(buttonIndex == 0)'用于,当用户按下'Yes'按钮时 – Krunal 2012-07-10 08:53:07

回答

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

设置第二个警报视图的委托为nil:

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

要么使用标签来对付像下面或内部alertView这是代表梅索德里面只是简单地设定代表零的情况,以便它永远不会打电话。

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

试试这个: -

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

      } 
     }