使用storyboard从appdelegate访问视图控制器

时间:2014-01-27 05:14:45

标签: ios7 uinavigationcontroller storyboard uitabbarcontroller appdelegate

我遵循了Ray Wendelich教程 RayWenderlichTutorial Tabbarcontroller using storyboards

只需查看故事板图片即可。 Ray正在使用以下代码'

访问“playerViewcontroller”
 UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UINavigationController *navigationController = [tabBarController viewControllers][0];
PlayersViewController *playersViewController = [navigationController viewControllers][0];

当我试图访问playerDetailsViewController时,使用以下代码我会崩溃。给予消息

  

评分[2630:60b] *由于未捕获的异常终止应用' NSRangeException',原因:' * - [__ NSArrayI objectAtIndex:]:索引1超出范围[0 .. 0]'   ***第一次抛出调用堆栈:   (0x30b49e83 0x3aea66c7 0x30a7fff9 0x3ddd1 0x3333eaad 0x3333e4f3 0x33338b41 0x332d3a07 0x332d2cfd 0x33338321 0x357b876d 0x357b8357 0x30b14777 0x30b14713 0x30b12edf 0x30a7d471 0x30a7d253 0x333375c3 0x33332845 0x3e0fd 0x3b39fab7)   libc ++ abi.dylib:以NSException类型的未捕获异常终止

代码

UITabBarController *tabBarController =
(UITabBarController *)self.window.rootViewController;
UINavigationController *navigationController =
[[tabBarController viewControllers] objectAtIndex:0];
PlayersViewController *playersViewController =
[[navigationController viewControllers] objectAtIndex:0];

UINavigationController *nav=[[navigationController viewControllers]objectAtIndex:1];
PlayerDetailsViewController *playerdetailsviewcontroller=[[nav viewControllers]objectAtIndex:0];

我也尝试了下面的代码,

UITabBarController *tabBarController =
(UITabBarController *)self.window.rootViewController;
UINavigationController *navigationController =
[[tabBarController viewControllers] objectAtIndex:0];
PlayersViewController *playersViewController =
[[navigationController viewControllers] objectAtIndex:0];
PlayerDetailsViewController *playerdetailsviewcontroller=[[navigationController viewControllers]objectAtIndex:1];

我的邮件

崩溃了
  

' NSRangeException',原因:' *** - [__ NSArrayI objectAtIndex:]:索引1超出界限[0 .. 0]'

故事板图片 enter image description here

基本上我想从appdelegate访问PlayerdetailsViewcontroller。 我想我错过了什么,有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您的应用在此崩溃(objectAtIndex:1):

UINavigationController *nav = [[navigationController viewControllers] objectAtIndex:1];
PlayerDetailsViewController *playerdetailsviewcontroller = [[nav viewControllers]objectAtIndex:0];

这是因为naviationController只有1个子视图(PlayersViewController)。如果不通过Segue在PlayerDetailsViewController内实例化PlayersViewController,您就无法进入故事板。

修改 您只能访问最初可见的ViewControllers(通过UITabBarController)。

我也不明白为什么你要向UINavigationController添加另一个PlayersViewController。如果您将PlayersViewControllerPlayerDetailViewController连接到Segue,navigationController将被重复使用(这将是正确的方法)。

答案 1 :(得分:0)

看起来你有一个从玩家到玩家详细信息的模态。所以你可以使用Players ViewController的presentViewController属性,就像这样

PlayersViewController *playersViewController = [navigationController.viewControllers firstObject];
UINavigationController *secondNavigationController = playersViewController.presentedViewController;
PlayerDetailsViewController *playerDetailsViewController = [secondNavigationController.viewControllers firstObject];