我正在尝试在NSUserdefault中存储信息。我的意图是,如果用户第一次启动应用程序,向用户显示弹出消息。我的代码如下:
//In my view did load method
self.prefs = [NSUserDefaults standardUserDefaults];
self.firstTimeLaunchingApp = [prefs integerForKey:@"firstTimeLaunchx"];
[self tableRefresh];
//This method is called after the refresh button is clicked
-(void)tableRefresh
{
[self displayFirstTimeMessage];
//Other codes ommited
}
//Method to display pop up if first time user
-(void) displayFirstTimeMessage
{
if (self.firstTimeLaunchingApp != 1 )
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Welcome!"
message:@"Thanks for using the app!"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
[prefs setInteger:1 forKey:@"firstTimeLaunchx"];
[prefs synchronize];
}
}
我在这里遇到的问题是,当我第一次启动应用程序时,警报弹出窗口出现了(这是预期的),但是当我点击刷新按钮(这将再次触发“displayFirstTimeMessage”)时,我仍然看到不符合预期的警报弹出窗口。我设置NSUserDefault键的方式有问题吗?
注意:如果我停止并重新启动应用程序,则不会出现弹出窗口。