Search code examples
iosobjective-ciphoneuinavigationcontrollerxcode-storyboard

how to access a UINavigationController(created in storyboard) in AppDelegate.m


UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
AllListsViewController *controller = navigationController.viewControllers[0];
controller.dataModel = _dataModel;

This is my former code. I can access the AllListsViewController through navigationController since it's the rootViewController.

But now my storyboard changed. I've tried to access the controller the same way, but it wouldn't work.

SWRevealViewController *revealViewController = (SWRevealViewController *)self.window.rootViewController;
UINavigationController *navigationController = (UINavigationController *)revealViewController.frontViewController;
AllListsViewController *controller = navigationController.viewControllers[0];

controller.dataModel = _dataModel;

Maybe it's because I set the frontViewController in storyboard so this revealViewController.frontViewController is nil. But I don't know other ways to access it from AppDelegate.m.

Sorry I can't post any images because of my reputation. Here's my code.https://github.com/liukaiyi54/CheckList

Somebody help me please. I've been tortured by this for days.

Thanks in advance!


Solution

  • Try this:

    //This is Main unless you have named your storyboard something different
      UIStoryboard *storyBoard =[ UIStoryboard storyboardWithName:@"Main" bundle:nil];
      //make sure to put this in "AllListsViewController" in your identifier in the storyboard
      UINavigationController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"AllListsViewController"];
      UINavigationController *myNavigationController = [[UINavigationController alloc] initWithRootViewController:vc];
      self.window.rootViewController = myNavigationController;
    

    This is where you would put your storyboard ID in the blue box:

    enter image description here