Access a view controller from appdelegate using st

2019-03-31 18:56发布

问题:

I have followed this Ray Wendelich Tutorial RayWenderlichTutorial Tabbarcontroller using storyboards

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];

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

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

the code

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];

I tried the below code also,

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

where i get a crash with message

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

The storyboard image

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

回答1:

Your app crashes here (objectAtIndex:1):

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

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.

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

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).



回答2:

It looks like you have a modal segue from Players to Player Details. So you could use the presentedViewController property of PlayersViewController, something like this

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