How to add a TabBar controller to template code?

Discussion in 'iOS Programming' started by Nekbeth, May 20, 2012.

  1. Nekbeth macrumors member


    Feb 20, 2011
    Vatican City
    Hi, I have a navigation controller. I created it a template from xcode (Universal App using Core Data), the implementation is the following:

    - (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 objectAtIndex: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;

    Now this is all works fine, I added views and conections using Storymode with out any problems until I try to Embed In a Tab Bar Controller to my Navitagation Controller. here is crash log:

     *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITabBarController topViewController]: unrecognized selector sent to instance 0x6e3c3b0'

    Obviously my TabBar is not set to be the topViewController, but because the template passes important data (managed Context). I don't want to mess around and move things.

    Any advice is appreciated,

    Thank you
  2. jnoxx macrumors 65816


    Dec 29, 2010
    Aartselaar // Antwerp // Belgium
    Obviously, you're have a UITabbarController somewhere.. And since you're trying to GET a topViewController of that object, even though you're casting.. That's why you're getting the runtime environment.
    You could make a Singleton (CoreDataManager), and put it in all the managedobjects and blah in there. That would save you alot of headeaches ^_-

Share This Page