使用storyboard从appdelegate访问视图控制器 [英] Access a view controller from appdelegate using storyboard
问题描述
我遵循了这个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
评级[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]'
故事板图片
The storyboard image
基本上我想从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
)。
我也不明白你为什么要添加另一个 UINavigationController
到 PlayersViewController
。如果您使用Segue navigationController
将 PlayersViewController
连接到 PlayerDetailViewController
将被重用(这将是正确的方法)。
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).
这篇关于使用storyboard从appdelegate访问视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!