iphone - 应用程序将进入后台 -> 转到 Root View 菜单

标签 iphone ios objective-c

我正在开发一个应用程序,它有大约 8 个 View 并使用导航 Controller 进行导航。第一个 View 是主菜单。

我想要的是(每个 View )如果用户按下主页按钮(应用程序确实进入后台)弹出到主视图。

我知道 AppDelegate 方法 applicationDidEnterBackgroundapplicationWillEnterForeground

而且我知道从导航 Controller 调用的方法 popToRootViewControllerAnimated

我尝试在 applicationDidEnterBackground 中使用 popToRootViewControllerAnimated。 喜欢:

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

但这行不通。

你能告诉我这份工作的最佳选择是什么吗?

最佳答案

我认为你可以像这样尝试 NSNotificationCenter:

applicationDidEnterBackgroundapplicationWillEnterForeground 里面放这个

[[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/

相关文章:

iphone - 如何创建同步版本的 NSURLConnection

iphone - 拒绝后删除应用内购买

ios - 如何忽略 iOS6 UITextField 中的占位符文本

ios - avplayer seek 在音频文件中播放错误的位置

ios - 如何成功更新用户资料

ios - 从天数中获取一周中的某一天的名称。 (iOS)

iphone - 放在哪里||

ios - [ dismissViewControllerAnimated :YES completion:nil]; not working for iOS device 7

objective-c - 在 iOS 应用程序中创建自定义模板

iphone - 如何解决 UITableView 滚动慢的问题