NSFetchRequest 将数据放入视图中?
我刚刚开始使用 CoreData API,并且正在学习一些教程。我了解了存储和检索对象的基础知识,但在按照 MVC 连接所有部分时遇到了困难。
我有一个 CustomView,我用 CoreAnimation 在其中绘制一些内容,包括一些将从 NSManagedObject 获取字符串的文本层。我从基本的 CoreData 应用程序模板开始,因此在 appDelegate 中声明了 ManagedObjectContext 等,我只是不确定应该如何将 CoreData 堆栈中的数据获取到视图中。顺便说一句,这都是代码,而不是界面生成器。
所以我的问题是,如果我想以纯 MVC 方式构建我的应用程序,我应该如何将数据从堆栈获取到视图中?例如,我应该如何让我的视图访问初始化的 NSManagedObjectContext?
我一直在阅读 Cocoa 设计模式,但对于 MVC 来说我有点笨。我知道这是一个非常普遍的问题,但如果有人可以说,“设置委托,抓住指针......”无论是什么,那就太好了!
提前致谢!
I am just getting started with the CoreData API and am following a few tutorials. I get the basics of storing and retrieving objects, but am having trouble connecting all the pieces in terms of MVC.
I have a CustomView into which I draw some stuff with CoreAnimation, including some text layers that will get their strings from an NSManagedObject. I started with a basic CoreData application template so the managedObjectContext etc are declared in the appDelegate, and I'm just not sure how I should be getting data from the CoreData stack into the view. By the way, this is all in code, not interface builder.
So my question is, if I want to build my app in a pure MVC way, how should I go about getting data from the stack into the view? How should I give my view access to the initialized NSManagedObjectContext, for example?
I have been reading Cocoa Design Patterns but am a bit of a dunce when it comes to MVC. I know its a pretty general question, but if someone can just say, "set the delegate, grab a pointer..." whatever it is, that would be great!
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以我做了一些更多的探索,似乎最简单的方法是获取指向 AppDelegate 的指针,然后获取指向 ManagedObjectContext 的指针。从那里,执行获取请求并将其放入您的视图中!
So I did some more poking around and it seems the easiest way to do this is to get a pointer to the AppDelegate and then a pointer to the managedObjectContext. From there, execute a fetch request and put it into your view!
在一个很好的教程中 Björn Sållarp 是这样做的
:在应用程序委托中,他创建 rootViewController 并向其发送上下文:
在 rootViewController 的 h 文件中,您声明:
创建其属性:
在 m you 中
然后它就在那里供您使用。
In a good tutorial Björn Sållarp does it this way:
From the app delegate he creates the rootViewController and sends it the context with:
In the rootViewController's h file you declare:
Create its property:
and in m you
Then its there for your use.