Universal Master-Detail Application with Core Data and tab controller for iphone storyboard gets unrecognized selector sent to instance error

StackOverflow https://stackoverflow.com/questions/16504305

Question

I started a Master-Detail Application in X-Code. I chose the options to be universal, core data, and git repo. When the application comes up, I went into the iphone story board, added a tab view controller, moved the nav/table/detail views it starts with to be in the tab controller as the third tab (in reality I want it to be the fourth). I then chose the tab controller to be the initial view the program should start with when in iphone mode. It builds successfully but does not allow the program to finish loading. The error that comes out is recorded below:

2013-05-11 21:35:00.302 FearlessAndThorough[6318:907] -[UITabBarController topViewController]: unrecognized selector sent to instance 0x1c592020
2013-05-11 21:35:00.306 FearlessAndThorough[6318:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITabBarController topViewController]: unrecognized selector sent to instance 0x1c592020'
*** First throw call stack:
(0x337f33e7 0x3b4ee963 0x337f6f31 0x337f564d 0x3374d208 0xc9e43 0x35662aa1 0x35662625    0x3565a833 0x35602d1f 0x356027ad 0x356021ef 0x3731a5f7 0x3731a227 0x337c83e7 0x337c838b  0x337c720f 0x3373a23d 0x3373a0c9 0x3565946d 0x356562b9 0xc9ab5 0x3b91bb20)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

I am hoping that someone has done this before and can give me a little insight as to the proper procedure or steps to take when setting up a tab view controller type app that will then convert into a master detail application for ipad.

Here is the current app delegate's didFinishLaunchingWithOptions:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
        UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
        splitViewController.delegate = (id)navigationController.topViewController;

        UINavigationController *masterNavigationController = splitViewController.viewControllers[0];
        MasterViewController *controller = (MasterViewController *)masterNavigationController.topViewController;
        controller.managedObjectContext = self.managedObjectContext;
    } else {
        UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
        MasterViewController *controller = (MasterViewController *)navigationController.topViewController;
        controller.managedObjectContext = self.managedObjectContext;

    }
    return YES;
}
Was it helpful?

Solution

Your problem is that you added a tab bar controller to the front of your iPhone storyboard, but in your "else" clause you say the root view controller of the window is a navigation controller -- it's not, the tab bar controller that you added is. If you put a log in as the second line in that else clause, you will see that navigationController is actually a tab bar controller, not a navigation controller. That else clause needs to look like this:

    UITabBarController *tbc = (UITabBarController *)self.window.rootViewController;
    UINavigationController *navigationController = tbc.viewControllers[3];
    MasterViewController *controller = (MasterViewController *)navigationController.topViewController;
    controller.managedObjectContext = self.managedObjectContext; 

I'm not sure about the "3" in tbc.viewControllers. I can't tell from your description which tab that navigation controller is in, so you might need to change that.

OTHER TIPS

I think I got your problem.

Did you use a UITabbar object in the Xcode navigator :

enter image description here

Because to manage views with a TabbarController, the best way is to embed your view in a Navigation Controller, like below :

enter image description here

Then you got your first view embedded in a navigation controller :

enter image description here

You can now add a new viewController in the field and add it to the TabbarController by control-dragging from the TabbarController and select the Relationship item :

enter image description here

Then you got 2 views in a navigationController :

enter image description here

And nothing to do in the appDelegate.

Then do the last step for any other view you want embedded in the navigationController.

Hope this helps.

// Change your else part in appdelegate to this it may works.

else {

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

    // For third view in tabbar controller

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

    navigationController = (UINavigationController *)self.window.rootViewController;
        MasterViewController *controller = (MasterViewController *)navigationController.topViewController;
        controller.managedObjectContext = self.managedObjectContext;

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top