Error "Unrecognized selector sent to instance"
Part of my AppDelegate code:
UITabBarController *tabBarController
= (UITabBarController *)self.window.rootViewController;
UINavigationController *navigationController
= [[tabBarController viewControllers] objectAtIndex:0];
PilotosViewController *playersViewController
= [[navigationController viewControllers] objectAtIndex:0];
playersViewController.drivers = players;
But I am getting this exception:
- [UIViewController viewControllers] : unrecognized selector posted to instance 0x6a75770
***
Application terminated due to uncaught 'NSInvalidArgumentException', reason: '- [UIViewController viewControllers]: unrecognized selector posted to instance 0x6a75770'
Where is the mistake?
source to share
I met the same problem because I followed the steps of the author, but
UINavigationController *navigationController
= [[tabBarController viewControllers] objectAtIndex:0];
this is what caused the crash because it is navigationController
not in index=0
, I swapped the locations of two tab bar items, then it works.
source to share
You need to make sure you are connecting things correctly in your XIB or storyboard. The exception indicates that the object is of type ViewController
when dispatched [tabBarController viewControllers]
and you expected UITabBarController
. This is why you get '-[ViewController viewControllers]:
. Make sure your root view controller is indeed a tab view controller.
source to share