我正在开发一个应用程序,它有大约 8 个 View 并使用导航 Controller 进行导航。第一个 View 是主菜单。
我想要的是(每个 View )如果用户按下主页按钮(应用程序确实进入后台)弹出到主视图。
我知道 AppDelegate 方法 applicationDidEnterBackground
和 applicationWillEnterForeground
。
而且我知道从导航 Controller 调用的方法 popToRootViewControllerAnimated
。
我尝试在 applicationDidEnterBackground 中使用 popToRootViewControllerAnimated。 喜欢:
[self.window.rootViewController.navigationController popToRootViewControllerAnimated:YES];
但这行不通。
你能告诉我这份工作的最佳选择是什么吗?
最佳答案
我认为你可以像这样尝试 NSNotificationCenter
:
在 applicationDidEnterBackground
和 applicationWillEnterForeground
里面放这个
[[NSNotificationCenter defaultCenter] postNotificationName:@"popToRoot" object:nil];
并在您的 rootViewController 的
viewDidLoad
(始终出现在应用程序启动时)中添加:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popToRootViewControllerAnimated) name:@"popToRoot" object:nil];
然后在您的 rootViewController
中创建一个方法:
- (void)popToRootViewControllerAnimated
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
每当应用程序第一次启动时,NSNotificationCenter
将初始化名称 popToRoot
并为此准备一个方法 popToRootViewControllerAnimated
。
当应用程序进入后台时,NSNotificationCenter
将传递消息 @"popToRoot"
给 rootViewController 的 popToRootViewControllerAnimated
方法和 viewcontroller
将弹出到 rootview
关于iphone - 应用程序将进入后台 -> 转到 Root View 菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18734529/