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

3 answers

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.



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.



You are obviously getting an object of a different type at index = 0.

If you are using a storyboard, go there and open Navigator> find a specific controller> see Relationships. This order can be used when referencing its viewControllers collection.



All Articles