JBeesky
JBeesky

Reputation: 319

Load Title ViewController When applicationDidBecomeActive:

I've created an app that has two viewcontrollers. The app opens to a title screen (general UIViewController titled 'Title') with a segue connection to the second view that is a custom class (OSViewController titled 'MapView'). As it is, the app suspends when entered into the background state so it opens right where you left off which is typically in MapView.

I want to know what I need to do to have the app start at the title screen when it becomes active. Preferably, I'd like it to open to the title screen if it is inactive for more than 1 minute. From what I've been reading, it seems like I would make a call in applicationDidBecomeActive: method in my AppDelegate to code this in. Please provide me the code to put in the applicationDidBecomeActive: method (if that's the right place to put it) that will reopen my app to the title screen when transitioning from the inactive state to the active state. My app is almost finished but I'd like to fix this issue and I don't have a lot of experience dealing with app states. Thanks in advance for your time.

If you need more information just ask.

Upvotes: 1

Views: 241

Answers (1)

dalton_c
dalton_c

Reputation: 7171

You can also register a class as an observer of the "didBecomeActive" notification. You should place this in the viewDidLoad or the init method of your class.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil]; 

In this case, willBecomeActive: is a method that you have defined in your class that get's called when the app becomes active again. That might look something like this:

- (void)willBecomeActive:(NSNotification *)notification {
    if (self.navigationController.topViewController == self) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

You'll also need to add this in your viewDidUnload method

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];

EDIT: Thanks @AMayes for the advice. I don't believe key/value observing is necessary in this instance.

Upvotes: 0

Related Questions