managedObjectContext is null if I set it as using property, but non-null if I set it as chained controller

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

سؤال

If I use the following code in order to set AppDelegate's managedObjectContext to the firstViewController, which is just a subclass of UITableViewController in AppDelegate.m's application: didFinishLaunchingWithOptions method,


    rootTabBarController *rootabbarcontroller = (rootTabBarController *)self.window.rootViewController;

    UINavigationController *navigationController = [[rootabbarcontroller viewControllers] objectAtIndex:0];
    FirstViewController *firstViewController = [[navigationController viewControllers] objectAtIndex:0];

    firstViewController.managedObjectContext = self.managedObjectContext;

and then run the simulator, the value of managedObjectContext is set to non-null within FirstViewController.m's viewDidLoad method, and hence the Core Data works as expected.

However, when I tried to write the following code in the application: didFinishLaunchingWithOptions: method:


_firstViewController.managedObjectContext = self.managedObjectContext;

and run the simulator, the resultant managedObjectProperty value within FirstViewController.m's viewDidLoad method is set to null.

So I wonder why the first implementation, which is chained from the root UITabBarController to root UINavigationController to FirstViewController works within its viewDidLoad: method, but the latter (just directly set to its property) doesn't.

It's worth noting that I declared @property (nonatomic, strong) FirstViewController *firstViewController; in AppDelegate.h when I tried to implement in the latter way. Also set its class to the appropriate view controller in storyboard.

I've read some blogs to take the latter approach (links are not handy, sorry), so it would be possible to take the latter route to set its managedObjectContext value correctly and synthesize Core Data with UITableView.

So what am I missing? And is it feasible to just take the latter approach (which saves me of a lot of coding and potential unexpected bugs)? And finally, which is the better way to take?

I use Xcode 5 and iOS 7, thanks.

[updated]

If I tried to add the following line as suggested by Almas in the answer below,

self.firstViewController = firstViewController;

then I got an compile error since firstViewController isn't defined there.

And so I added the initialization of the firstViewController, by adding the following line just above the above line.

FirstViewController *firstViewController = [[FirstViewController alloc] init];

but then the AppDelegate.m looks like working correctly, but this time, an error occurred in viewDidLoad method of the FirstViewController.m, which is the same as my first reported one.

هل كانت مفيدة؟

المحلول

Did you set your local variable firstViewController to ivar/property before setting managed object context to it?

add:

self.firstViewController = firstViewController;
// or
_firstViewController = firstViewController;

before:

_firstViewController.managedObjectContext = self.managedObjectContext;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top