iOS - 如何从核心数据实体创建视图
我已经在我的应用程序中实现了核心数据,并且我正在尝试找出从模型对象创建视图的最佳方法。
我的 Cat 模型对象具有以下属性:
@property (nonatomic, retain) NSString * image;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSString * desc;
我想创建一个 Cat 视图,将图像显示为 UIImage,并将标题和描述显示为标签。我需要使用模型代理吗?我认为 KVO 也会参与其中。有人知道这方面的好教程吗?这似乎是一项常见任务,但我能找到的唯一教程仅涉及核心数据,并且不解释如何从数据创建视图对象。
I have implemented Core Data in my app, and I'm trying to figure out the best way to create views from the model objects.
My Cat model object has these properties:
@property (nonatomic, retain) NSString * image;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSString * desc;
I want to create a Cat view that displays the image as a UIImage, and displays the title and description as labels. Do I need to use a model proxy? I think KVO will be involved as well. Anyone know of a good tutorial for this? It seems like this would be a common task but the only tutorials I can find are about Core Data only and do not explain how to create view objects from the data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
Controller
对象将 CatModel
对象与 CatView
对象关联起来。你需要查找有关 MVC 的教程。
您可能想到的是 Binding。将模型的任何更改绑定到视图。 Cocoa 有绑定,但仅在 Mac OS 中:
可可绑定
来自:从 Cocoa 移植
另请查看
NSFetchedResultsController
。这可能也很有趣。您必须创建自己的绑定类。我们在我参与的一个旧 iOS 项目中就有一个。
Binding 类将使用 KVO 并具有源键路径、目标键路径、源对象和目标对象。在初始化时,您将在源(您的模型)上设置 KVO 观察器,当它发生更改时,您可以使用目标键路径将该更改传播到目标(您的视图)。您将在控制器对象中创建 Biding - 因为视图和模型不应该直接相互通信(糟糕的 MVC)
You associate your Cat
Model
object with your CatView
object using aController
object.You need to look up tutorials on MVC.
You probably have Binding's in mind. Where you bind any changes to the Model to the View. Cocoa has bindings, but only in Mac OS:
Cocoa Bindings
That was from: Porting from Cocoa
Also take a look at
NSFetchedResultsController
. This might be of interest too.You would have to create your own bindings class. We had one in an old iOS project I worked on.
The Binding class would use KVO and have a source key path, a destination key path, source object and destination object. On init you would setup your KVO observer on the source (your Model) and when it changes, you propagate that change to the destination (your View) using the destination key path. You would create the Biding in your controller object - as the View and Model shouldn't talk to each other directly (bad MVC)