iphonecocoa-touchioscore-dataios-3.x

TabBar application, CoreData related application crash


In my project I'm using a tabBarController as the rootView Controller, then on one of my tabs, I add my exsisting ToDoList application. The problem I'm having is this: If I use this code in the AppDelegate: ToDoList is load as RootView. But I want it to show only after appropriate tab selected.

- (void)applicationDidFinishLaunching:(UIApplication *)application {  
    //todoRootController.managedObjectContext = self.managedObjectContext;
    ToDoRootViewController *todoRootViewController = [[ToDoRootViewController alloc]initWithNibName:@"ToDoRootViewController" bundle:nil];
    NSManagedObjectContext *context = [self managedObjectContext];

    if (!context) {
        // Handle the error.
    }

    // Pass the managed object context to the view controller.
    todoRootViewController.managedObjectContext = context;
    UINavigationController *aNavigationController = [[UINavigationController alloc]                                                  
                                                     initWithRootViewController:todoRootViewController];
    self.navigationController = aNavigationController;

    [window addSubview:[navigationController view]];
    [window makeKeyAndVisible];
    [todoRootViewController release];

    [aNavigationController release];
}

The I replace applicationDidFinishLaunching:(UIApplication *)application method as CoreDataReceipeis sample code

- (void)applicationDidFinishLaunching:(UIApplication *)application {  
    todoRootController.managedObjectContext = self.managedObjectContext;

//////////////// same stuff
}

But then it gives 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Task''


Solution

  • Utilize Interface Builder to set the Tab View Controllers that will be displayed when a tab is typed.

    If you must do it programmatically then you should check out the documentation for TabBarControllers.

    of

    - (void)applicationDidFinishLaunching:(UIApplication *)application { 
    
      tabBarController = [[UITabBarController alloc] init]; 
    
      MyViewController* vc1 = [[MyViewController alloc] init]; 
    
      MyOtherViewController* vc2 = [[MyOtherViewController alloc] init]; 
    
      NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, nil]; 
    
      tabBarController.viewControllers = controllers; 
    
      // Add the tab bar controller's current view as a subview of the window 
      [window addSubview:tabBarController.view];
    
    }