弹出到上一个 UINavigationController
Pop to previous UINavigationController
我有两个像这样的 UINavigationControllers:
LoginNavigationController -> LoginView
HomeNavigationController -> HomeView -> ...
我从 LoginView 导航到 HomeView,如下所示:
[self presentViewController:HomeNavigationController animated:YES completion:nil];
现在,当应用进入后台时,我需要转到 LoginView。这样做的正确方法是什么?使用 presentViewController 重定向会导致任何内存问题吗?
假设您的LoginViewController
是层次结构中的根视图控制器,也许您可以在您的应用程序委托中的rootViewController
上调用popToRootViewController
(在applicationWillEnterForeground
或其他内容中)
您可以使用视图控制器popToRootViewController
。首先,你需要找到你想去的前一个控制器,
在您的 HomeNavigationController 上,
for (UIViewController *controller in self.navigationController.viewControllers) {
if([controller isKindOfClass:[LoginNavigationController class]]){
[self.navigationController popToViewController:controller animated:YES];
break;
}
}
当应用程序进入后台然后进入前台时执行此代码。
呈现可能不是您想要的,因为它呈现的是另一个视图控制器,而不是已经加载的视图控制器。我会跳转到根视图控制器。
[self.navigationController popToRootViewControllerAnimated:YES];
在您 AppDelegate
的 applicationDidEnterBackground
方法中更改 window 的根视图控制器。
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Change the rootViewController of your window..
}
请在登录视图中使用以下代码 -
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(methodEnterBGMode) name:UIApplicationDidEnterBackgroundNotification object:nil];
}
- (void)methodEnterBGMode
{
[self dismissViewControllerAnimated:NO completion:nil];
}
希望对您有所帮助。
我有两个像这样的 UINavigationControllers:
LoginNavigationController -> LoginView
HomeNavigationController -> HomeView -> ...
我从 LoginView 导航到 HomeView,如下所示:
[self presentViewController:HomeNavigationController animated:YES completion:nil];
现在,当应用进入后台时,我需要转到 LoginView。这样做的正确方法是什么?使用 presentViewController 重定向会导致任何内存问题吗?
假设您的LoginViewController
是层次结构中的根视图控制器,也许您可以在您的应用程序委托中的rootViewController
上调用popToRootViewController
(在applicationWillEnterForeground
或其他内容中)
您可以使用视图控制器popToRootViewController
。首先,你需要找到你想去的前一个控制器,
在您的 HomeNavigationController 上,
for (UIViewController *controller in self.navigationController.viewControllers) {
if([controller isKindOfClass:[LoginNavigationController class]]){
[self.navigationController popToViewController:controller animated:YES];
break;
}
}
当应用程序进入后台然后进入前台时执行此代码。
呈现可能不是您想要的,因为它呈现的是另一个视图控制器,而不是已经加载的视图控制器。我会跳转到根视图控制器。
[self.navigationController popToRootViewControllerAnimated:YES];
在您 AppDelegate
的 applicationDidEnterBackground
方法中更改 window 的根视图控制器。
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Change the rootViewController of your window..
}
请在登录视图中使用以下代码 -
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(methodEnterBGMode) name:UIApplicationDidEnterBackgroundNotification object:nil];
}
- (void)methodEnterBGMode
{
[self dismissViewControllerAnimated:NO completion:nil];
}
希望对您有所帮助。