在按钮点击时关闭rootViewController

时间:2016-01-22 05:37:52

标签: objective-c viewcontroller dismiss rootviewcontroller

我在应用程序启动时通过storyboard实例化viewController,这是混合应用程序的一部分。最重要的是,我提出了一个视图控制器。现在虽然解雇了presentedViewController我也想解雇rootViewController,以便显示混合应用屏幕。我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:1)

[self dismissViewControllerAnimated:YES completion:^{


 // after your second view controller dismissed.
 // set your new view controller as a root of window.
 // You need to set navigation controller and set any root view controller for that navigation controller in storyboard.
// also don't forget to set identifier of your navigation controller.


   UINavigationController* rootController = [[UIStoryboard storyboardWithName:kStoryboardName bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"controllerIdentifier"];

   UIWindow* window = [[UIApplication sharedApplication] keyWindow];
   window.rootViewController = rootController; 

  // this will set your new navigation controller with root view on UIWindow.

}];

答案 1 :(得分:1)

好的,我对Worklight一无所知。或者什么是混合应用程序。所以这可能没有意义。但这是对你的问题的严格的iOS答案。

在不将其替换为另一个视图控制器的情况下解除根视图控制器是没有意义的。如果你这样做,你会让你的应用程序无法与它进行交互(除了可能会摇动手机?)。

所以没有办法像使用子视图控制器那样解除它。但你可以删除它。

UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
[keyWindow.rootViewController.view removeFromSuperview];
keyWindow.rootViewController = nil;

答案 2 :(得分:0)

NSMutableArray *viewControllers = [self.navigationController.viewControllers mutableCopy];
[viewControllers removeObjectAtIndex:0];
[self.navigationController setViewControllers:viewControllers];