如何从我的视图控制器引用我的应用程序委托中的 ManagedObjectContext?

发布于 2024-08-21 07:33:01 字数 1250 浏览 5 评论 0原文

我知道还有其他问题涉及该主题,但似乎没有一个问题完全符合我所经历的情况,因此提出了新问题。

我有一个应用程序,它是 UITabBarController,我定义了两个子视图,

两个选项卡的 Identity Inspector 中的 Class 属性都设置为 UINavigationController。

两个子视图都是 UIViewController 类并包含 MKMapView 对象。

我正在尝试集成核心数据,目标是我可以使用核心数据来存储有关我想要放置在地图上的对象的信息。

我在 Delegate 头文件中将 UITabBarController 定义为“rootController”。 我还在那里定义了管理对象模型、管理对象上下文和持久存储协调器属性。

在 Delegate 实现中,我具有上述属性的标准访问器方法,并且 rootController 定义如下:

- (void)applicationDidFinishLaunching:(UIApplication *)application {

[window addSubview:rootController.view];
[window makeKeyAndVisible];

}

在子视图的视图控制器中,我定义了 ManagedObjectContext 并合成了它。

现在,对于我的问题,我无法让编译器允许我从视图控制器引用应用程序委托中的 ManagedObjectContext 。

我在 applicationDidFinishLaunching 方法中尝试了以下操作:

firstView.managedObjectContext = self.managedObjectContext;

但我收到以下错误:

Accessing unknown 'setManagedObjectContext:' class method

任何人都可以帮我解决这个问题吗?

谢谢


更新:

为了添加我的问题,我将通过提供更多详细信息来阻止一些答案

我的 appdelegate.h 文件中有一个 @Class 声明 我的 appdelegate.m 文件中有一个用于 firstView.h 文件的 #import 语句 我已在 appdelegate.h 文件中声明了我的firstView,如下所示

FirstView *firstView;

I know there are other questions covering this topic, but none seem to fit exactly what I'm experiencing, hence the new question.

I have an app which is a UITabBarController, I have defined two subviews

Both tabs have their Class attribute in the Identity Inspector set to UINavigationController.

Both subviews are Class UIViewController and contain MKMapView objects.

I am trying to integrate Core Data with the objective being that I can use Core Data to store information about object I want to place on the map.

I have my UITabBarController defined as 'rootController' in my Delegate header file.
I also have the managedObjectModel, managedObjectContext and persistentStoreCoordinator properties defined there.

In the Delegate implementation I have the standard accessor methods for the above properties and I have rootController defined as follows:

- (void)applicationDidFinishLaunching:(UIApplication *)application {

[window addSubview:rootController.view];
[window makeKeyAndVisible];

}

In my view controllers for the child views, I have defined my managedObjectContext and synthesized it.

Now for my problem, I cannot get the compiler to allow me to reference the managedObjectContext in the App Delegate from the View Controllers.

I tried the following in the applicationDidFinishLaunching method:

firstView.managedObjectContext = self.managedObjectContext;

But I just get the following error:

Accessing unknown 'setManagedObjectContext:' class method

Can anyone help me figure this out?

Thanks


Update:

To add to my question, I'll head off some answers by providing more detail

I have an @Class declaration in my appdelegate.h file
I have a #import statement in my appdelegate.m file for the firstView.h file
I have declared my firstView as follows in my appdelegate.h file

FirstView *firstView;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

扛刀软妹 2024-08-28 07:33:01

firstView 是如何声明和赋值的?

它应该是对具有名为“managementObjectContext”属性的对象的引用。

How is firstView declared and assigned?

It should be a reference to an object with a property named "managedObjectContext".

小矜持 2024-08-28 07:33:01

我也遇到了同样的问题,我将分享我的解决方案。

首先,您需要在 nib 文件的选项卡栏中引用导航控制器,确保将其连接起来。

IBOutlet UINavigationController *navigationController;

然后,按照支持文档中的建议获取控制器并向其发送 ManagedObjectContext:

SavedTableViewController *saved = (SavedTableViewController *)[navigationController topViewController];
saved.managedObjectContext = self.managedObjectContext;

Alex(来自另一篇文章)是对的,“您通常应该远离从应用程序委托获取共享对象。这使得它的行为太像全局变量,这会带来一大堆与之相关的问题。”

I've ran into this same problem, i'll share my solution.

First you need a reference to the Nav Controller in the Tab Bar in the nib file, make sure you connect it up.

IBOutlet UINavigationController *navigationController;

Then, get the Controller as recommended in the support docs and send it the managedObjectContext:

SavedTableViewController *saved = (SavedTableViewController *)[navigationController topViewController];
saved.managedObjectContext = self.managedObjectContext;

Alex (from another post) is right, "You should generally stay away from getting shared objects from the app delegate. It makes it behave too much like a global variable, and that has a whole mess of problems associated with it."

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