2014-10-20 51 views
0

我试图实现的是当用户在3次后打开应用程序时显示UIAlertview。我在ViewDidAppear的ViewController中使用下面的代码,但是每次打开应用程序时都会显示UIAlertview。有人能告诉我我在这里做错了吗?3启动后显示UIAlertview应用程序

int launches = [[NSUserDefaults standardUserDefaults] integerForKey:@"launchCount"]; 
if (launches > 3) { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"My Alert" 
                message:@"Some message" delegate:nil 
             cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
} 
[[NSUserDefaults standardUserDefaults] setInteger:launches+1 forKey:@"launchCount"]; 

编辑:我也得到一个NSInteger(又名'长')'int'警告。这可能是为什么它不起作用的问题吗?

enter image description here

+1

要修复警告,请将'int'更改为'NSInteger'。 – AdamPro13 2014-10-20 20:17:19

+0

[显示uialertview 3次后打开应用程序?]的可能重复?(http://stackoverflow.com/questions/26471780/show-uialertview-after-opening-an-app-after-3-times)什么目的是有两次问相同的问题? – Popeye 2014-10-30 13:16:57

回答

0

你应该代码转移到应用程序委托中 -application:didFinishLaunchingWithOptions:

+0

不会,每次打开应用程序时,仍然会显示警告和警报视图弹出窗口。 – Jan 2014-10-20 20:21:44

+0

检查首次启动时启动的返回值。此外,您是否希望在第三次启动后或第三次启动后每次启动应用程序时都会显示警报? – AdamPro13 2014-10-20 20:24:52

+0

如何检查返回的值?是的,我希望每次在第3次发布后启动应用程序时都会展示它。 – Jan 2014-10-20 20:27:51

0

@ AdamPro13是正确的,你应该在你的应用程序代理移动代码,但可能是- (void)applicationDidBecomeActive:(UIApplication *)application。对于每次应用发布,可以多次调用viewDidAppear方法。如果您想在每次安装时仅显示UIAlertView一次,则可以保存BOOL(如果已显示)或更改测试launches == 4。像

NSInteger launches = [[NSUserDefaults standardUserDefaults] integerForKey:@"launchCount"]; 
BOOL launchAlertShown = [[NSUserDefaults standardUserDefaults] boolForKey:@"launchAlertShown"]; 
if (launches > 3 && !launchAlertShown) { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"My Alert" 
               message:@"Some message" delegate:nil 
            cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"launchAlertShown"]; 
} 
[[NSUserDefaults standardUserDefaults] setInteger:launches+1 forKey:@"launchCount"]; 

东西,你必须删除的应用,不只是一个新的版本覆盖,删除的[NSUserDefaults standardUserDefaults]内容。这意味着测试launches > 3将保持为真,直到您从您正在测试的设备或模拟器删除应用程序。,

0

知道了!感谢@ AdamPro13

NSInteger launches = [[NSUserDefaults standardUserDefaults] integerForKey:@"launchCount"]; 
    if (launches % 3 == 0) { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"My Alert" 
                 message:@"Some message" delegate:nil 
               cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 
    } 

    [[NSUserDefaults standardUserDefaults] setInteger:launches+1 forKey:@"launchCount"]; 
相关问题