仅显示一次警报消息,但在应用程序启动时再次出现
我正在尝试找出一种方法,当我的一个视图控制器被选中时,让一条消息出现在我的一个视图控制器上(这个视图不是应用程序上的第一个视图,所以我不能在启动).我只希望它出现一次,并且在重新选择该视图时不再出现;但是,如果应用程序重新启动,我希望它再次出现.
I'm trying to figure out a way to get a message to appear on one of my view controllers when that screen controller is selected (this view is NOT the first view on the app, so I can't do it at start-up). I only want it to appear ONCE and not appear again when that view is re-selected; however, I want it to appear again if the app is restarted.
到目前为止,我能够让它毫无问题地出现一次,并且在使用该应用程序时它不会再次弹出,这是我想要的,因为我经常返回其他视图并返回.我遇到的问题是,当我再次启动应用程序时,我希望在选择适当的视图时出现警报消息,但由于存储在 NSUserDefaults 中的数据,它不会出现.
So far, I am able to get it to appear once without any problem AND it won't pop up again while using the app, which is what I want as I am often going back into other views and returning. The problem I am having, is when I start the app up again, I want the alert message to appear when I select the appropriate view, but because of the data stored in NSUserDefaults it won't appear.
有什么想法吗?这出现在 ViewWillAppear 中:
Any ideas? This appears in ViewWillAppear:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *alertshown = [defaults stringForKey:@"alertshown"]; {
if (alertshown == nil) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Press the Code button" message:@"Take a photo of this screen and show it to get your code" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
[defaults setObject:@"alertwasdisplayed" forKey:@"alertshown"];
}}
根据您的喜好,您需要使用应用程序的 APPDelegate 的:
Based on your preference, you need use you app's APPDelegate's:
- (void)applicationDidEnterBackground:(UIApplication *)application;
此委托方法会在您的应用进入后台时通知您.您在委托方法中编写的代码将是:
This delegate method will let you know when your app goes into background. The code you write in the delegate method would be:
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"alertshown"];
希望会有所帮助.