Displaying an alert message only ONCE but to reappear again on app launch

不羁岁月 提交于 2019-12-13 06:42:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!