How to change RootViewController in AppDelegate From Other ViewController?

后端 未结 3 579
清酒与你
清酒与你 2021-02-07 11:41

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 li

相关标签:
3条回答
  • 2021-02-07 12:32

    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**"];
    
    0 讨论(0)
  • 2021-02-07 12:33

    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.

    0 讨论(0)
  • 2021-02-07 12:42

    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:enter image description here

    0 讨论(0)
提交回复
热议问题