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

只需查看情节提要图像即可。
雷正在使用以下代码访问“ playersViewcontroller”

 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 0x30b370337003b3703d3a 03b3d3a 03d3a 03d3a 03d3a 03d3a 03d3a 03d3a 0d3a3d3a 03d3a 0d3d3a 0a3d3a 0e3d3a 0d3f3d3d0b3d0d
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]'


故事板图片


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

最佳答案

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

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


发生这种情况是因为naviationController只有1个子视图(PlayersViewController)。如果不通过Segue实例化PlayerDetailsViewController中的PlayersViewController,则无法深入情节提要。

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

我也不明白为什么要在UINavigationController中添加另一个PlayersViewController。如果使用Segue将PlayersViewController连接到PlayerDetailViewController,将重新使用navigationController(这是正确的方法)。

10-06 05:16