How to change RootViewController in AppDelegate From Other ViewController?

女生的网名这么多〃 提交于 2019-12-20 18:01:36

问题


This is didFinishLaunchingWithOptions Method in AppDelegate. Let me explain scenario, I have developed sideMenu like facebook in my app, but now I have to change the sideMenu list according to screens (ViewController)

Here the side Menu is SideMenuViewController, which is an argument in contain, which ultimately becomes window's rootViewController.

SO, The very basic question arises is "How to change the controller or variable which becomes rootViewController of windows"

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

SideMenuViewController *leftMenuViewController = [[SideMenuViewController alloc] init];

self.container = [ContainerOfSideMenuByVeerViewController
                  containerWithCenterViewController:[self navigationController]
                  leftMenuViewController:leftMenuViewController];

self.window.rootViewController = self.container;

[self.window makeKeyAndVisible];

return YES;

}

If any programmer wants to know more code or requirement, I do welcome to provide by editing my code or in comments.


回答1:


Try this:

<YourAppDelegateClass> *app = [[UIApplication sharedApplication] delegate];
app.window.rootViewController = <YourRootViewController>;

Don't forget to include necessary headers (your AppDelegate, for example) or you'll get CE. This one seems to work:




回答2:


With Storyboard

Inside another ViewController you can use this code:

self.view.window.rootViewController = [self.view.window.rootViewController.storyboard   instantiateViewControllerWithIdentifier:@"**STORYBOARD ID**"];

Inside AppDelegate:

self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"**STORYBOARD ID**"];



回答3:


Knowledge Sharing Using Swift:

Changing root view controller from class other than app delegate.swift

let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var homeViewController = mainStoryboard.instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController
let nav = UINavigationController(rootViewController: homeViewController)
appdelegate.window!.rootViewController = nav

Changing rootviewcontroller With Animation can be achieve with:

UIView.transitionWithView(self.window!, duration: 0.5, options: UIViewAnimationOptions.TransitionFlipFromLeft, animations: {
      self.window?.rootViewController = anyViewController
}, completion: nil)

We can write generalise method too similar to this.

Hope this will helpful for someone.



来源:https://stackoverflow.com/questions/17408171/how-to-change-rootviewcontroller-in-appdelegate-from-other-viewcontroller

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