问题
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.
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.
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"];
}}
回答1:
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"];
Hope that will help.
来源:https://stackoverflow.com/questions/11083067/displaying-an-alert-message-only-once-but-to-reappear-again-on-app-launch