Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application will enter background -> go to root view menu

I am developing an application which has about 8 views and use navigation controller to navigate through. The first view is a main menu.

What i want is (of each view) to pop to the main view if the user press the home button (App did enter background).

I know the AppDelegate methods applicationDidEnterBackground and applicationWillEnterForeground.

And i know the method popToRootViewControllerAnimated called from the navigation controller.

I have tried to use popToRootViewControllerAnimated in applicationDidEnterBackground. Like:

[self.window.rootViewController.navigationController popToRootViewControllerAnimated:YES];

But this does not work.

Can you please let me know what is the best option for this job?

like image 599
q0re Avatar asked Sep 11 '13 06:09

q0re


People also ask

How do I set root view in Swift?

plist file → Application Scene Manifest property → Scene Configuration → item 0 and get rid of the property Storyboard Name by clicking the icon that has a minus in the circle next to it. ► Run the app and you will see the black screen and let's change that by setting a new root view controller.

What is root view controller in Swift?

The root view controller provides the content view of the window. Assigning a view controller to this property (either programmatically or using Interface Builder) installs the view controller's view as the content view of the window.


1 Answers

i think you try NSNotificationCenter like this:

inside applicationDidEnterBackground and applicationWillEnterForeground put this

[[NSNotificationCenter defaultCenter] postNotificationName:@"popToRoot" object:nil];

and in your rootViewController's viewDidLoad (that always appears on app launch) add this:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popToRootViewControllerAnimated) name:@"popToRoot" object:nil];

Then create a method in your rootViewController:

- (void)popToRootViewControllerAnimated
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

Whenever the application will start first time, NSNotificationCenter will initialize for name popToRoot and prepare a method popToRootViewControllerAnimated for this.

And when application will go to background, NSNotificationCenter will pass a massage @"popToRoot" to rootViewController's popToRootViewControllerAnimated method and viewcontroller will pop to rootview

like image 156
Vaibhav Saran Avatar answered Oct 20 '22 14:10

Vaibhav Saran