如何在 AppDelegate.m 中访问 UINavigationController(在故事板中创建)

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;

这是我以前的代码。我可以通过 navigationController 访问 AllListsViewController,因为它是 rootViewController。

但现在我的故事板变了。我试过用同样的方法访问控制器,但是不行。

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

controller.dataModel = _dataModel;

可能是因为我在 storyboard 中设置了 frontViewController 所以这个 revealViewController.frontViewController 是 nil。但我不知道从 AppDelegate.m.

访问它的其他方法

抱歉,由于我的声誉,我无法 post 任何图像。这是我的代码。https://github.com/liukaiyi54/CheckList

请有人帮助我。我被这个折磨了好几天了

提前致谢!

试试这个:

//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;

这是您将故事板 ID 放在蓝色框中的位置: