使用故事板从 appdelegate 访问视图控制器 [英] Access a view controller from appdelegate using storyboard

查看:36
本文介绍了使用故事板从 appdelegate 访问视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了这个 Ray Wendelich 教程RayWenderlichTutorial Tabbarcontroller 使用故事板

I have followed this Ray Wendelich Tutorial RayWenderlichTutorial Tabbarcontroller using storyboards

只需查看故事板图像即可.Ray 正在使用以下代码访问playersViewcontroller"

Just see the storyboard image. Ray is accesing 'playersViewcontroller using the below code'

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

当我尝试访问 playerDetailsViewController 时,使用以下代码我崩溃了.发消息

When I'm trying to access playerDetailsViewController, using the following code i get a crash. Giving the message

Ratings[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 类型异常终止

Ratings[2630:60b] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]' *** First throw call stack: (0x30b49e83 0x3aea66c7 0x30a7fff9 0x3ddd1 0x3333eaad 0x3333e4f3 0x33338b41 0x332d3a07 0x332d2cfd 0x33338321 0x357b876d 0x357b8357 0x30b14777 0x30b14713 0x30b12edf 0x30a7d471 0x30a7d253 0x333375c3 0x33332845 0x3e0fd 0x3b39fab7) libc++abi.dylib: terminating with uncaught exception of type 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]'

'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

故事板图像

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

Basically i want to access the PlayerdetailsViewcontroller from appdelegate. I think I'm missing something, any thoughts?

推荐答案

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

Your app crashes here (objectAtIndex:1):

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

发生这种情况是因为 naviationController 只有 1 个子视图 (PlayersViewController).如果不通过 Segue 实例化 PlayersViewController 中的 PlayerDetailsViewController,您就无法深入故事板.

This happens because naviationController has only 1 subview (PlayersViewController). You can't go further down the storyboard without instantiating the PlayerDetailsViewController within PlayersViewController via a Segue.

编辑您只能访问最初可见的 ViewControllers(通过 UITabBarController).

EDIT You can only access the ViewControllers that are initially visible (via the UITabBarController).

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

I also don't get why you're adding another UINavigationController to the PlayersViewController. If you connect PlayersViewController to PlayerDetailViewController with a Segue the navigationController will be reused (this would be the correct way to do so).

这篇关于使用故事板从 appdelegate 访问视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆