【问题标题】:UIAlertView Show OnceUIAlertView 显示一次
【发布时间】:2012-07-08 20:16:28
【问题描述】:

我知道这听起来可能很奇怪,我在以前的应用程序中使用过它,但现在在我现在的应用程序中无法使用相同的代码使其工作。我试图在应用程序启动时仅显示一次警报视图 uisng:

 if (![@"1" isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:@"alert"]]){

    [[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:@"alert"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}



 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title" message:@"Text here" delegate:nil cancelButtonTitle: @"Ok" otherButtonTitles: nil];

[alert show];

如果我在这里做了一些愚蠢的事情,有人介意检查一下吗,我已经将它与我的应用进行了交叉检查,这一切似乎都一样。

【问题讨论】:

    标签: objective-c ios xcode uialertview


    【解决方案1】:

    在 if 块内显示警报

    if (![@"1" isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:@"alert"]]){
    
        [[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:@"alert"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title" message:@"Text here" delegate:nil cancelButtonTitle: @"Ok" otherButtonTitles: nil];
    
        [alert show];
    
    }
    

    或使用

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"alertShownOnce"] == NO)
    {
       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title" message:@"Text here" delegate:nil cancelButtonTitle: @"Ok" otherButtonTitles: nil];
        [alert show];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"alertShownOnce"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    

    【讨论】:

    • 当然!非常感谢您的回复,快速回复排序!
    • 我要等 9 分钟才能让我接受答案,它没有考虑到您的快速响应:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多