0

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

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

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

4

1 回答 1

5

在 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];
}
于 2012-07-08T20:18:05.553 回答