2012-09-04 47 views
0

我有一個if語句在應用程序委託的application DidFinishLaunchingWithOptions。即使if語句不正確,if語句中的代碼也會運行。難道我做錯了什麼?它似乎忽略了if語句。應用程序didFinishLaunchingWithOptions忽略如果語句

- (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; 
} 
+0

如果您登錄「我」它適當增加? –

+0

@NDPostWhenIdle我沒有嘗試過,但我只是,並輸出控制檯6.我不知道如何或爲什麼發生。 –

+0

好吧,所以條件'i> = 3'是真的,但警報沒有顯示? –

回答

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