Reputation: 11
I'm making a Core Data based iPhone application which stores some data.
It has a UITabBarController
as the root view controller (RootViewController
). The tab bar controller is given two view controllers by the application delegate - the first one being an instance of UIViewController
which represents the title screen of the app and the second one being a UITableViewController
which is used to display the data.
This is my first iPhone application using Core Data. I've read that the proper way of building this kind of apps is to create and initialize the managedObjectModel
, managedObjectContext
and persistentStoreCoordinator
objects in the application delegate and then pass the managedObjectContext
to the child view controllers by reference. This is how I did it.
However, I didn't manage to pass the managedObjectContext
object into the tab bar controller which I initialize in my application delegate's applicationDidFinishLaunching:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions {
RootViewController *rootViewController = [[RootViewController alloc] init];
rootViewController.managedObjectContext = self.managedObjectContext;
[window addSubview:rootViewController.view];
[window makeKeyAndVisible];
[rootViewController release];
return YES;
}
Even though the tab bar is displayed properly and loads the title screen view controller, its managedObjectContext
remains nil. I wasn't able to figure out what am I doing wrong. I also tried to pass the RootViewController
a string by adding a new retained property to it.
My RootViewController.h
reads as it follows:
@interface RootViewController : UITabBarController {
@private
NSManagedObjectContext *managedObjectContext;
}
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@end
My RootViewController's viewDidLoad method:
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"%@", self.managedObjectContext);
ObiadViewController *obiadVC = [[ObiadViewController alloc] init];
ObiadListNavController *obiadListVC = [[ObiadListNavController alloc] init];
obiadVC.managedObjectContext = self.managedObjectContext;
obiadListVC.managedObjectContext = self.managedObjectContext;
NSArray *vcs = [NSArray arrayWithObjects:obiadVC, obiadListVC, nil];
self.viewControllers = vcs;
[obiadVC release];
[obiadListVC release];
}
I also checked that the managedObjectContext
is not nil in the application delegate, just before it gets passed to the RootViewController
instance. It's like all RootViewController
's ivars get reset. It happens only at this point. When I pass a string from the table view controller to the detail view controller later on, everything is just fine.
I hope I made myself clear. I'm feeling pretty clueless at the moment.
Upvotes: 1
Views: 1541
Reputation: 161
UITabBarController Class Reference clearly states that UITabBarController isn't supposed to be subclassed:
This class is not intended for subclassing.
In this case you could instantiate your UITabBarController and add view controllers to it in your App Delegate's applicationDidFinishLaunching:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
tabBarController = [[UITabBarController alloc] init];
FirstViewController *firstViewController = [[FirstViewController alloc] init];
SecondViewController *secondViewController = [[SecondViewController alloc] init];
firstViewController.managedObjectContext = self.managedObjectContext;
secondViewController.managedObjectContext = self.managedObjectContext;
NSArray *vcs = [NSArray arrayWithObjects:firstViewController, secondViewController, nil];
tabBarController.viewControllers = vcs;
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
[firstViewController release];
[secondViewController release];
return YES;
}
Hope it helps.
Upvotes: 2