Reputation: 1359
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?
Upvotes: 5
Views: 8729
Reputation: 6115
First: you should check wether the rootviewcontroller is a navigationController. Because self.window.rootViewController.navigationController is often nil. Why? Because the navigationController of a navigationController is 'nil'. Mostly, I set my rootViewController to be a navigationController
Secondly: You shouldn't do animated stuff when your application is about to quit. You should do it not-animated
popToRootViewControllerAnimated:NO
Upvotes: 0
Reputation: 12908
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
Upvotes: 10
Reputation: 2430
have you tries it like this :-
[self.navigationController popToRootViewControllerAnimated:YES];
replace your navigationController name with navigationController here.
Edit:-
in AppDelegate.h file
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UINavigationController *navMain;
}
@property (nonatomic, retain) UINavigationController *navMain;
in AppDelegate.m file
@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 edited anwser
Upvotes: 2
Reputation: 1024
crate a propery for UINavigationController in your AppDelegate class. In applicationDidEnterBackground: method call the popToRootViewController method by using the UINavigationController Property. For Suppose your propery name is navigationController then
[self.navigationController popToRootViewControllerAnimated:YES];
Upvotes: 0