应用程序didFinishLaunchingWithOptions忽略if语句

时间:2012-09-04 01:27:00

标签: ios if-statement appdelegate

我在App Delegate的application DidFinishLaunchingWithOptions中有一个if语句。即使if语句不为true,if语句中的代码也会运行。难道我做错了什么?它似乎忽略了if语句。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ 
NSInteger i = [[NSUserDefaults standardUserDefaults] integerForKey:@"numOfLCalls"];
[[NSUserDefaults standardUserDefaults] setInteger:i+1 forKey:@"numOfLCalls"];

if (i >= 3) {
    UIAlertView *alert_View = [[UIAlertView alloc] initWithTitle:@"Hey! You are still coming back!" message:@"It would mean a whole lot to me if you rated this app!" delegate:self cancelButtonTitle:@"Maybe later" otherButtonTitles: @"Rate", nil];
    [alert_View show];
}

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}

1 个答案:

答案 0 :(得分:2)

您将此值存储到NSUserDefaults中,在重建应用时不会清除该值。要重置此号码,您必须从模拟器或设备卸载应用程序并重建。

NSUserDefaults的要点是它确实是持久的。即使您的应用程序是从应用程序商店更新的,它仍将保留,并且清除其数据的唯一两种方法是专门并故意删除您引用的密钥,或删除该应用程序。

此外,正如您在下面所见,我为您做了一些轻微的调整:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if (![[NSUserDefaults standardUserDefaults] integerForKey:@"numOfLCalls"]) {
        [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"numOfLCalls"];
    }else{
        NSInteger i = [[NSUserDefaults standardUserDefaults] integerForKey:@"numOfLCalls"];
        [[NSUserDefaults standardUserDefaults] setInteger:i++ forKey:@"numOfLCalls"];
    }

    if (i >= 3) {
        UIAlertView *alert_View = [[UIAlertView alloc] initWithTitle:@"Hey! You are still coming back!" message:@"It would mean a whole lot to me if you rated this app!" delegate:self cancelButtonTitle:@"Maybe later" otherButtonTitles: @"Rate", nil];
        [alert_View show];
    }

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}