使用 Storyboard 时获取 NSManagedObjectContext
目标是获取当前的 NSManagedObjectContext 以便使用 Core Data。在 iOS 4.3 中,我将 UINavigationController 的委托设置为 AppDelegate,如下所示(在 AppDelegate.m 中):
self.navigationController.delegate = self;
并且我可以执行类似的操作(无论我需要上下文):
NSManagedObjectContext *context = [self.navigationController.delegate performSelector:@selector(managedObjectContext)];
现在,在 iOS 5 中,我正在使用 Storyboard,并且我'我很难弄清楚如何实现这一目标。我首先使用了委托,因为我认为您不想一直传递 AppDelegate.h。
The objective is to get the current NSManagedObjectContext in order to work with Core Data. In iOS 4.3 I set the UINavigationController's delegate to be the AppDelegate like so (in AppDelegate.m):
self.navigationController.delegate = self;
and I could do something like this (wherever I needed the context):
NSManagedObjectContext *context = [self.navigationController.delegate performSelector:@selector(managedObjectContext)];
Now, in iOS 5, I am using a Storyboard and I'm having a difficult time figuring out how to achieve this. I used a delegate in the first place because I don't think you want to be passing your AppDelegate.h around all the time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@Rose - 再来一次?即使是苹果公司也强烈反对:
来自苹果 Doc :
推荐方式:
@Rose - Again? It is highly discouraged even by Apple:
From Apple Doc:
Recommended way:
我不知道这是否是您所需要的,但它可能会有所帮助:
id appDelegate = (id)[[UIApplication sharedApplication] delegate];
self.managedObjectContext = [appDelegate ManagedObjectContext];
I don't know if this is what you need, but it may help:
id appDelegate = (id)[[UIApplication sharedApplication] delegate];
self.managedObjectContext = [appDelegate managedObjectContext];