将 ManagedObjectContext 传递给 UITabBarController 问题
我正在制作一个基于 Core Data 的 iPhone 应用程序,它存储一些数据。
它有一个 UITabBarController
作为根视图控制器 (RootViewController
)。应用程序委托为选项卡栏控制器提供了两个视图控制器 - 第一个是 UIViewController
的实例,代表应用的标题屏幕,第二个是 UITableViewController
> 用于显示数据。
这是我的第一个使用 Core Data 的 iPhone 应用程序。我读到构建此类应用程序的正确方法是在应用程序中创建并初始化 managementObjectModel
、managementObjectContext
和 persistentStoreCoordinator
对象委托,然后通过引用将 ManagedObjectContext 传递给子视图控制器。我就是这样做的。
但是,我没有设法将 managedObjectContext
对象传递到我在应用程序委托的 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;
}
即使选项卡栏正确显示并加载标题屏幕视图控制器,其 managedObjectContext
保持为零。我无法弄清楚我做错了什么。我还尝试通过添加新的保留属性来向 RootViewController 传递一个字符串。
我的 RootViewController.h
的内容如下:
@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];
}
我还检查了应用程序委托中的 managementObjectContext
在传递给RootViewController
实例。就像所有 RootViewController 的 ivars 都被重置一样。它只发生在此时。当我稍后将字符串从表视图控制器传递到详细视图控制器时,一切都很好。
我希望我说清楚了。我现在感觉很无知。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
UITabBarController 类参考 明确指出UITabBarController 不应该被子类化:
在这种情况下,您可以实例化 UITabBarController 并在应用程序代理的 applicationDidFinishLaunching 中向其添加视图控制器:
希望它有帮助。
UITabBarController Class Reference clearly states that UITabBarController isn't supposed to be subclassed:
In this case you could instantiate your UITabBarController and add view controllers to it in your App Delegate's applicationDidFinishLaunching:
Hope it helps.