The application will enter the background & # 8594; go to the root view menu

I am developing an application that has about 8 views and uses a navigation controller to navigate. The first view is the main menu.

What I want is (of each view) to pop up on the main screen if the user presses the home button (the application does enter the background).

I know the AppDelegate applicationDidEnterBackground and applicationWillEnterForeground methods.

And I know the popToRootViewControllerAnimated method, which is called from the navigation controller.

I tried using popToRootViewControllerAnimated in applicationDidEnterBackground. How:

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

But that will not work.

Could you tell me what is the best option for this job?

+4
source share
4 answers

I think you will try NSNotificationCenter as follows:

inside applicationDidEnterBackground and applicationWillEnterForeground put this

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

and in your rootViewController's viewDidLoad (which always appears when the application starts) add this:

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

Then create a method in rootViewController :

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

Whenever the application is launched for the first time, NSNotificationCenter initializes the name popToRoot and prepares the popToRootViewControllerAnimated method for it.

And when the application goes to the background, NSNotificationCenter will pass the massage method @"popToRoot" to rootViewController popToRootViewControllerAnimated and viewcontroller will appear in rootview

+9
source

do you try to do this: -

 [self.navigationController popToRootViewControllerAnimated:YES]; 

replace the name of navigationController with navigationController here.

Edit: -

in the file AppDelegate.h

 @interface AppDelegate : UIResponder <UIApplicationDelegate> { UINavigationController *navMain; } @property (nonatomic, retain) UINavigationController *navMain; 

in the file AppDelegate.m

 @synthesize navMain; -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease]; self.navMain = [[UINavigationController alloc] initWithRootViewController:self.viewController]; self.window.rootViewController = self.navMain; [self.window makeKeyAndVisible]; return YES; } -(void)applicationDidEnterBackground:(UIApplication *)application { NSLog(@"applicationDidEnterBackground"); [self.navMain popToRootViewControllerAnimated:YES]; } 

try editing anwser

+2
source

First: you must verify that the rootviewcontroller is a navigation controller. Because self.window.rootViewController.navigationController is often zero. What for? Because the navigationController of the navigation controller is "nil". Basically, I found that rootViewController is a navigation controller

Secondly: You should not do animated things when your application is about to exit. You have to make it non-animated

 popToRootViewControllerAnimated:NO 
0
source

enter the UINavigationController class in the AppDelegate class. In applicationDidEnterBackground: the method is called by the popToRootViewController method using the UINavigationController property. Suppose your own name is navigationController, then

  [self.navigationController popToRootViewControllerAnimated: YES]; 
0
source

Source: https://habr.com/ru/post/1501594/


All Articles