应用程序将进入后台 - >转到根视图菜单 [英] Application will enter background -> go to root view menu
问题描述
我正在开发一个具有大约8个视图并使用导航控制器进行导航的应用程序。第一个视图是主菜单。
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).
我知道AppDelegate方法 applicationDidEnterBackground
和 applicationWillEnterForeground
。
I know the AppDelegate methods applicationDidEnterBackground
and applicationWillEnterForeground
.
我知道从导航控制器调用的方法 popToRootViewControllerAnimated
。
And i know the method popToRootViewControllerAnimated
called from the navigation controller.
我试过在applicationDidEnterBackground中使用popToRootViewControllerAnimated。
喜欢:
I have tried to use popToRootViewControllerAnimated in applicationDidEnterBackground. Like:
[self.window.rootViewController.navigationController popToRootViewControllerAnimated:YES];
但这不起作用。
能告诉我这项工作的最佳选择吗?
Can you please let me know what is the best option for this job?
推荐答案
我想你会尝试 NSNotificationCenter
这样:
applicationDidEnterBackground
和 applicationWillEnterForeground
把这个
[[NSNotificationCenter defaultCenter] postNotificationName:@"popToRoot" object:nil];
和你的 rootViewController的
viewDidLoad
(总是出现在应用启动时)添加:
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];
然后在 rootViewController
中创建一个方法:
Then create a method in your rootViewController
:
- (void)popToRootViewControllerAnimated
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
每当应用程序首次启动时, NSNotificationCenter
将初始化名称 popToRoot
并为此准备一个方法 popToRootViewControllerAnimated
。
Whenever the application will start first time, NSNotificationCenter
will initialize for name popToRoot
and prepare a method popToRootViewControllerAnimated
for this.
当应用程序转到后台时, NSNotificationCenter
将通过按摩 @popToRoot
到 rootViewController的popToRootViewControllerAnimated
方法和 viewcontroller
将弹出到 rootview
And when application will go to background, NSNotificationCenter
will pass a massage @"popToRoot"
to rootViewController's popToRootViewControllerAnimated
method and viewcontroller
will pop to rootview
这篇关于应用程序将进入后台 - >转到根视图菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!