4

Part of my AppDelegate code is:

UITabBarController *tabBarController 
    = (UITabBarController *)self.window.rootViewController;

UINavigationController *navigationController 
    = [[tabBarController viewControllers] objectAtIndex:0];

PilotosViewController *playersViewController 
    = [[navigationController viewControllers] objectAtIndex:0];

playersViewController.drivers = players;

But I get this exception:

-[UIViewController viewControllers]: unrecognized selector sent to instance 0x6a75770
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController viewControllers]: unrecognized selector sent to instance 0x6a75770'

Where is the mistake?

El Sinior X
  • 71
  • 1
  • 2
  • 4
  • 1
    How do you know that this is the section of code that is causing the problem? Have you set breakpoints and tracked the code execution with the debugger? The error says that you sent viewControllers to a UIViewController instance, but your code snippet only shows a UITabBarController and UINavigationController instances receiving that message. – Peter M Mar 20 '12 at 12:44

3 Answers3

11

I met the same issue, because I followed the steps by the author, but

UINavigationController *navigationController 
    = [[tabBarController viewControllers] objectAtIndex:0];

this is what which made the crash, because navigationController is not at index=0, I did exchange the locations of the two tab bar items, then it works.

user229044
  • 232,980
  • 40
  • 330
  • 338
comic
  • 111
  • 2
  • thanks.. i just stucked for half an hour. search by myself can't find the answer.. read your answer at STO, solved my problem. :) i vote your answer. – Alfred Angkasa Oct 24 '12 at 08:39
1

You need to make sure that you connect things properly in your XIB or storyboard. The exception is showing you that the object is of type ViewController when you send [tabBarController viewControllers] and you were expecting a UITabBarController. That's why you're getting '-[ViewController viewControllers]:. Make sure that your root view controller really is a tab view controller.

wbyoung
  • 22,383
  • 2
  • 34
  • 40
0

You are obviously receiving different type of object on index = 0.

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

Viktor Kucera
  • 6,177
  • 4
  • 32
  • 43