【问题标题】:how to access a UINavigationController(created in storyboard) in AppDelegate.m如何在 AppDelegate.m 中访问 UINavigationController(在情节提要中创建)
【发布时间】:2016-01-02 04:36:27
【问题描述】:
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;

可能是因为我在故事板中设置了frontViewController,所以这个revealViewController.frontViewController 为nil。但我不知道从 AppDelegate.m 访问它的其他方法。

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

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

提前致谢!

【问题讨论】:

  • 使用storyboard ID并根据storyboard ID调用你的视图控制器
  • @Mr.T 工作正常。谢谢。

标签: ios objective-c iphone uinavigationcontroller xcode-storyboard


【解决方案1】:

试试这个:

//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 放入蓝色框中的位置:

【讨论】:

  • 我试过了,还是不行。我认为该方法 instantiateViewControllerWithIdentifier: 创建了一个新控制器。我不想要一个新的,因为我已经在我的情节提要中创建了一个,并且我无法再次设置 rootViewController,因为我已经在情节提要中有一个 rootViewController。不确定我是否理解正确。我想做的就是将数据保存到 iPhone。我可以在不添加 SWRevealViewController 的情况下做到这一点,但我现在真的迷失了。如果你有时间,也许你可以看看我的代码。谢谢你的回答!
  • 你到底想做什么? AppDelegate 通常用于告诉您的应用程序在启动时或收到令牌时进入视图等。
  • 我只想保存数据。在添加 SWRevealViewController 之前我做得很好。现在我完全迷路了。
  • 成功了,虽然还是有点小问题,但我想我会尽力解决的。谢谢您的回答。你真的很好!!
猜你喜欢
  • 2012-10-25
  • 1970-01-01
  • 1970-01-01
  • 2012-12-05
  • 2014-12-26
  • 2015-02-09
  • 2014-08-23
  • 1970-01-01
相关资源
最近更新 更多