Move to another storyboard without a UINavigationController

◇◆丶佛笑我妖孽 提交于 2019-12-25 07:19:02

问题


I've got two storyboards. I found some code on how I can instantiate the initial View Controller of another Storyboard and move (or do you say segue?) to that. But those examples used a Navigation Controller. The user shouldn't be able to go back to the previous Storyboard in my case (login page). The app can forget about the old Storyboard, it's not needed ever again when I move over to the new one.

So the View Controller on top of the hierarchy in the first Storybard can be forgotten about, I don't want it to be the presenter of the next Storyboard. It shouldn't exists, the presenter and top of hierarchy should now be the initial View Controller of the second Storyboard.

How can I do this?


回答1:


To do this, you need to:

  1. Instantiate the second storyboard
  2. Instantiate the desired view controller from the second storyboard
  3. change the rootController of the window to the new view controller

This is how you do it:

//Instantiate the second Storyboard
UIStoryboard *secondStoryboard = [UIStoryboard storyboardWithName:@"SecondStoryBoard" bundle:nil]; 

//Instantiate new VC, you will need to set the Storyboard Identifier of the VC to @"postLoginRoot" in your second storyboard
UIViewController *newRootVC = [secondStoryboard instantiateViewControllerWithIdentifier:@"postLoginRoot"];

//swap the root view controllers of the window
//nb: change the animation type as you see fit
UIAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
UIWindow *mainWindow = delegate.window;

[mainWindow  insertSubview:newRootVC.view belowSubview:mainWindow.rootViewController.view];

[UIView transitionWithView:mainWindow
                      duration:0.8
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                        [mainWindow.rootViewController.view removeFromSuperview];
                    }
                    completion:^(BOOL completed){
                        mainWindow.rootViewController=newRootVC;
                    }];


来源:https://stackoverflow.com/questions/22576535/move-to-another-storyboard-without-a-uinavigationcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!