Ios – “Unrecognized selector sent to instance” after casting rootViewController to UINavigationController *

exceptioniosobjective c

Not too sure how to debug this.

2013-01-24 20:36:18.448 SlideMenu[2069:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[initViewController viewControllers]: unrecognized selector sent to instance 0xac6cdb0'

Here's initViewController.m

#import "initViewController.h"
#import "ECSlidingViewController.h"
#import "MenuViewController.h"

@interface initViewController ()

@end

@implementation initViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Main"];
}

@end

And where the exception is being thrown:

AppDelegate.m

#import "AppDelegate.h"
#import "MainViewController.h"
#import "ListDoc.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ListDoc *list1 = [[ListDoc alloc] initWithTitle:@"Potato Bug" thumbImage:[UIImage imageNamed:@"potatoBugThumb.jpg"]];
    ListDoc *list2 = [[ListDoc alloc] initWithTitle:@"House Centipede" thumbImage:[UIImage imageNamed:@"centipedeThumb.jpg"]];
    NSMutableArray *lists = [NSMutableArray arrayWithObjects:list1,list2,nil];

    UINavigationController * navController = (UINavigationController *) self.window.rootViewController;
    MainViewController * mainController = [navController.viewControllers objectAtIndex:0];
    mainController.someData = lists;
    // Override point for customization after application launch.
    return YES;
}

@end

Best Answer

From Your Post :

2013-01-24 20:36:18.448 SlideMenu[2069:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[initViewController viewControllers]: unrecognized selector sent to instance 0xac6cdb0'

Found where the exception is being thrown:

UINavigationController * navController = (UINavigationController *) self.window.rootViewController;  
MainViewController * mainController = [navController.viewControllers objectAtIndex:0];

Here is my reading of that : The item navControlleris an instance of initViewController and this is probably not what you are expecting.
initViewController is probably not a subclass of UINavigationController.

How To Debug ? Try This :
NSLog(@"%@", [navController class]);

Related Topic