使用 Storyboard 时获取 NSManagedObjectContext

发布于 2024-12-10 13:36:13 字数 476 浏览 0 评论 0原文

目标是获取当前的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

远山浅 2024-12-17 13:36:13

@Rose - 再来一次?即使是苹果公司也强烈反对:

来自苹果 Doc

视图控制器通常不应该从全局对象(例如应用程序委托)检索上下文,这使得应用程序架构变得僵化。视图控制器也不应该创建供自己使用的上下文(除非它是嵌套上下文)。这可能意味着使用控制器上下文执行的操作未注册到其他上下文,因此不同的视图控制器对数据会有不同的视角。

推荐方式:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
MasterViewController *controller = (MasterViewController *)navigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
return YES;
}

@Rose - Again? It is highly discouraged even by Apple:

From Apple Doc:

A view controller typically shouldn’t retrieve the context from a global object such as the application delegate—this makes the application architecture rigid. Neither should a view controller create a context for its own use (unless it’s a nested context). This may mean that operations performed using the controller’s context aren’t registered with other contexts, so different view controllers will have different perspectives on the data.

Recommended way:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
MasterViewController *controller = (MasterViewController *)navigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
return YES;
}
怎樣才叫好 2024-12-17 13:36:13

我不知道这是否是您所需要的,但它可能会有所帮助:
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];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文