7

我是 iPhone 开发人员的新手,

我想一个接一个地实现 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];
    } 
}

写完这段代码后,我进入了无限循环。

任何帮助将不胜感激。

4

4 回答 4

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

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 2)
    {
        //Do something
    }
    else
    {
        //Do something else
    }
}
于 2012-07-10T08:34:21.520 回答
4

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

UIAlertView* alertew = [[UIAlertView alloc] initWithTitle:@"Deleted Successfully !"
                                                            message:nil delegate:nil 
                                                  cancelButtonTitle:@"OK" otherButtonTitles:nil];
于 2012-07-10T08:41:42.613 回答
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];
} 
}
于 2012-07-10T08:53:15.907 回答
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");
                }

            }
        }
于 2015-04-17T09:37:54.150 回答