UIAlertView委托中的UIAlertView Click事件

时间:2012-07-10 08:32:05

标签: iphone ipad uialertview buttonclick

我是iPhone开发人员的新手,

我希望一个接一个地实现2个警报视图,例如当用户按下删除按钮时,第一个警报视图会通过两个按钮Are you sure want to Delete ?yes

询问no

现在,如果用户按下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];
    } 
}
编写此代码后,我在无限循环中。

我们将不胜感激。

4 个答案:

答案 0 :(得分:11)

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

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

答案 1 :(得分:4)

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

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

答案 2 :(得分:1)

使用标签来解决下面的情况,或者只是为委托方法中的内部alertView设置Delegate nil,以便它永远不会调用。

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

答案 3 :(得分: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");
                }

            }
        }