14

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.

Chatar Veer Suthar
  • 15,541
  • 26
  • 90
  • 154

3 Answers3

14

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

Renderhp
  • 386
  • 2
  • 6
  • Dear I already tried this, it brings to me to main windows screen, but dear I have already pushed and navigated two Controllers inside than main controller, so ? – Chatar Veer Suthar Jul 01 '13 at 15:39
  • well, you can get access to stack of controllers in your navigation controller using `.navigationController.viewControllers`. It returns NSArray. And try to replace first controller in this array with your new one. – Renderhp Jul 01 '13 at 15:50
  • Not exactly, but your way helped me alot to resolve my problem. Thanks @renderhp – Chatar Veer Suthar Jul 03 '13 at 02:25
  • Dear Can you help me with this http://stackoverflow.com/questions/17960034/how-to-change-the-rootviewcontroller – YasBES Jul 31 '13 at 01:36
  • @Renderhp:I change my rootViewcontroller by your idea but in newrootview I can't perform any action. Please help me. – TamilKing Oct 28 '13 at 12:08
  • For newbies, don't forget to use `[app.window makeKeyAndVisivle]` after changing root controller. – Azri Jamil Nov 02 '14 at 06:53
14

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**"];
Anton Gaenko
  • 8,929
  • 6
  • 44
  • 39
11

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.

MobileGeek
  • 2,462
  • 1
  • 26
  • 46