UIAlertView - 是/否条件

时间:2010-07-03 05:32:29

标签: iphone

我的应用程序需要警告消息,如果按下按钮,则再一次警告消息,然后我必须调用一个方法。这是我的代码:

-(IBAction)resetPressed:(id)sender
{
    NSString *title= [NSString stringWithFormat:@"Warning"];
    NSString *message = [NSString stringWithFormat:@"Are you sure you want to Reset"];
    NSString *ok = [NSString stringWithFormat:@"No"];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                    message:message
                                                   delegate:self 
                                          cancelButtonTitle:ok otherButtonTitles:@"Yes",nil];
    [alert show];
    [alert release];
}


- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (alertView.tag ==1)
    {
        NSString *title= [NSString stringWithFormat:@"Warning"];
        NSString *message = [NSString stringWithFormat:@"Are you sure you want to Reset"];
        NSString *ok = [NSString stringWithFormat:@"No"];

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                        message:message
                                                       delegate:self 
                                              cancelButtonTitle:ok otherButtonTitles:@"Yes",nil];
        alert.tag =2;
        [alert show];
        [alert release];

    }
    else if(alertView.tag ==2)
    {
        [self resetArray];
    }
}

感谢。

3 个答案:

答案 0 :(得分:1)

我不确定你的目标是什么,但不管怎么说,我看起来有些不对劲:

首先,您应该以这种方式创建字符串:

NSString *title= @"Warning";

您的案例中无需使用stringWithFormat

然后,似乎你没有正确地将第一个UIAlert的标签设置为1,标签的默认值是0,所以我想if中的didDismissWithButtonIndex语句永远不会成立。

此外,您应该使用buttonIndex检查按下了哪个按钮,否则您将同时显示警告并致电[self resetArray],无论用户按下哪个按钮。

希望有所帮助。

答案 1 :(得分:1)

在您的代码中,您创建了第一个警报,但实际上从未在其上设置标记。你应该这样做:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                    message:message
                                                   delegate:self 
                                          cancelButtonTitle:ok otherButtonTitles:@"Yes",nil];

alert.tag = 1; //Or 2, or something.
[alert show];
[alert release];

然后代理方法中的代码将运行。

答案 2 :(得分:0)

请在.h文件中定义两个单独的UIAlertView

@interface XYZViewController:UIViewController
{
     UIAlertView  *firstAlertView;
     UIAlertView  *secondAlertView;
}

现在在您的.m文件中修改如下:

-(IBAction)resetPressed:(id)sender
{
    NSString *title= [NSString stringWithFormat:@"Warning"];
    NSString *message = [NSString stringWithFormat:@"Are you sure you want to Reset"];
    NSString *ok = [NSString stringWithFormat:@"No"];

    if(firstAlertView == nil)
    {
       firstAlertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:ok otherButtonTitles:@"Yes",nil];
    }
    [firstAlertView show];

}

 - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (alertView == firstAlertView)
    {
        NSString *title= [NSString stringWithFormat:@"Warning"];
        NSString *message = [NSString stringWithFormat:@"Are you sure you want to Reset"];
        NSString *ok = [NSString stringWithFormat:@"No"];

        if(secondAlertView == nil)
        {
            secondAlertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:ok otherButtonTitles:@"Yes",nil];
        }
        [secondAlertView show];
    }
    else if(alertView == secondAlertView)
    {
        [self resetArray];
    }
}

并且在dealloc方法中请释放分配的UIAlertviews。

希望我很清楚。

谢谢,
吉姆。