Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
407 views
in Technique[技术] by (71.8m points)

iphone - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...