序列化/存储UIView及其子视图 [英] Serializing/Storing a UIView and its subviews

查看:165
本文介绍了序列化/存储UIView及其子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个相对较新的iOS开发人员,我以前的大部分经验来自.NET。
我的应用程序是一个画布系统,有一个父UIView包含用户放置/调整大小/等作为子视图的所有对象。我想要能够保存这些位置/配置到命名文件。

I'm a relatively new iOS developer, with most of my previous experience coming from .NET. My application is a canvas like system in that there is a parent UIView that contains all the objects the user is placing/resizing/etc as subviews. I want to be able to save these positions/configurations to named files.

在.NET中,我将简单地子类化UIView,给它一个title属性,然后序列化这个文件,然后反序列化他们加载它们,但在Cocoa我很丢了。

In .NET, I would have simply subclassed UIView, given it a "title" property, then serialized this to file, and then deserialized them to load them back, but in Cocoa I'm quite lost.

原来我想我可以使用NSCoding,但这个对于多个文件保存来说似乎不是一个好的解决方案。

Originally I thought I could do this using NSCoding, but this doesn't seem to be a good solution for multiple file saving.

所以我查看了Core Data,但我不知道如何创建Core Data对象现有的Cocoa UIKit类,如UIView。

So I looked at Core Data, but I'm not sure how I can create Core Data objects of existing Cocoa UIKit classes like UIView.

我已经花了一段时间搜索,找不到任何关于这种困境的信息。
我应该使用什么,最好的方法是什么?

I've spent a while googling and can't find any information about this kind of predicament. What should I use, and what is the best way to go about it?

推荐答案

可以使用NSKeyedArchiver,因为UIView实现NSCoding。然而,更多的面向MVC的设计将是让用户通过移动视图来操纵数据模型。要保存,您需要归档模型,而不是视图本身。

You probably could do it with NSKeyedArchiver, since UIView implements NSCoding. A more MVC-oriented design, however, would be to have the user manipulate a data model by moving the views around. To save, you'd archive the model rather than the views themselves.

这篇关于序列化/存储UIView及其子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆