ICL1901
ICL1901

Reputation: 7778

iOS - passing a null managed object -

I've read a lot of similar posts but after two days, I thought I should ask my own question.

I have a separate CoreData Controller. This passes the entity object fine from AppDelegate to the RootViewController. It does not pass it to a specific (Category) view controller, and I cant figure out why.

The code in App Delegate where I try to pass the object is this:

rootViewController.managedObjectContext = self.coreDataController.mainThreadContext;
    categoryListViewController.managedObjectContext = self.coreDataController.mainThreadContext;
    NSLog(@"AD/core data controller is %@", coreDataController.mainThreadContext);
    NSLog(@"AD- rootVC is %@", rootViewController.managedObjectContext);
    NSLog(@"AD/category list is %@", categoryListViewController.managedObjectContext);

and the logs show that the core data controller and the root vc get populated, but the Category vc doesn't.

2012-12-02 14:28:33.187 [50351:907] AD/coredatacontroller moc is <NSManagedObjectContext: 0x21065160>
2012-12-02 14:28:33.188 [50351:907] AD/categorycontroller moc is (null)
2012-12-02 14:28:33.190 [50351:907] AD- rootVC moc is <NSManagedObjectContext: 0x21065160>

Any ideas why?

UPDATE

If I do as suggested by Valentin, and init the Category VC in the App Delegate, I certainly get the managed objects passed through, however, as I call the view from the Detail VC. When I do that, I get the error "Application tried to push a nil view controller on target ".

If I try to init the category VC (and load the context) in the detail VC, it does not convey, and the logs show the context to be nil.

Init the VC (in App Delegate):

categoryListViewController = [[CategoryListViewController alloc] initWithNibName:@"CategoryList-iPad" bundle:nil];

                        // we have loaded from our xib, so has our CoreDataController,
    // so connect as its delegate and setup its persistent store
    //
    self.coreDataController.delegate = self;
    [self.coreDataController loadPersistentStores];



    UINavigationController *rootNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
    UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController];



    // Set up MASTER and DETAIL delegation so we can send messages between views
    rootViewController.detailViewController = detailViewController;
    detailViewController.rootViewController = rootViewController;

    splitViewController = [[UISplitViewController alloc] init];

    splitViewController.viewControllers = @[rootNavigationController, detailNavigationController];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

    splitViewController.delegate = detailViewController;

    rootViewController.managedObjectContext = self.coreDataController.mainThreadContext;
    categoryListViewController.managedObjectContext = self.coreDataController.mainThreadContext;


    NSLog(@"AD - coreDataController is %@", coreDataController.mainThreadContext);
    NSLog(@"AD - rootViewController is %@", rootViewController.managedObjectContext);
    NSLog(@"AD - categoryListVC is %@", categoryListViewController.managedObjectContext);

Call the view (in DetailViewController):

 -(void)categoryButtonTapped {
     NSLog(@"%s", __FUNCTION__);

     //categoryListViewController = [[CategoryListViewController alloc] initWithNibName:@"CategoryList-iPad" bundle:nil];
     //categoryListViewController.managedObjectContext = coreDataController.mainThreadContext;
     //categoryListViewController.managedObjectContext = self.coreDataController.mainThreadContext;

 UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:categoryListViewController];
     nc.modalPresentationStyle = UIModalPresentationFormSheet;


      NSLog(@"DVC FRC is %@", self);
      NSLog(@"DVC FRC/moc is %@", coreDataController.mainThreadContext);
      NSLog(@"DVC FRC/self.moc is %@", self.coreDataController.mainThreadContext);

     [self presentViewController:nc animated:YES completion:nil];

     //[self.navigationController pushViewController:categoryListViewController animated:YES];
}

Upvotes: 0

Views: 216

Answers (1)

Rad&#39;Val
Rad&#39;Val

Reputation: 9231

Most probably your categoryListViewController is nil as well. Try to see if it gets alloc'ed/initialised correctly.

Upvotes: 1

Related Questions