当视图包含带有自定义图像的 UIButton 时,UIViewControllerencodeWithCoder 失败
基本上,我的问题正是标题中所说的。当我尝试对 UIViewController
的子类进行编码时,调用 [superencodeWithCoder]
会给出 NSInvalidArgumentException
。具体来说,我得到 -[UIImageencodeWithCoder:]: unrecognized selector sent to instance XxXXXXXX
。
视图中唯一的图像位于 UIButton
上,它也应该符合 NSCoding,并且堆栈跟踪包含对 [UIBUttonencodeWithCoder]
的调用。该按钮是使用 [UIButton buttonWithType:UIButtonTypeCustom]
以编程方式创建的,并且使用 setImage: forState:
设置图像。我真的不知道这里发生了什么事。我是否遗漏了一些明显的东西,或者 UIButton
只是不真正符合 NSCoding
?
Basically, my problem is exactly what it says in the title. When I try to encode a subclass of UIViewController
, calling [super encodeWithCoder]
gives an NSInvalidArgumentException
. Specifically, I get -[UIImage encodeWithCoder:]: unrecognized selector sent to instance XxXXXXXX
.
The only image image in the view is on a UIButton
, which is also supposed to conform to NSCoding, and the stack trace includes a call to [UIBUtton encodeWithCoder]
. The button is created programmatically with [UIButton buttonWithType:UIButtonTypeCustom]
, and the image is set with setImage: forState:
. I really have no idea what is going on here. Am I missing something obvious, or does UIButton
just not really conform to NSCoding
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法想象为什么您想要在应用程序中存档视图对象,但您可能需要通过在 UIImage 上编写类别来自行添加 NSCoding 支持。
有关详细信息,请参阅: iPhone - 为什么文档说 UIImageView NSCoding 兼容吗?
视图对象支持 NSCoding,因为视图加载系统使用它从 Nib 文件加载对象。但是 UINib 类做了一些 NSKeyedArchiver 没有做的额外工作。
如果您只想存储启动之间的状态,最好存储数据而不是查看状态。特别是如果您计划更新应用程序并对视图布局进行任何更改。 (这是模型/视图/控制器分离背后的动机。)
I can't imagine why you would want to be archiving view objects within your app, but you probably need to add NSCoding support yourself by writing a category on UIImage.
For details see: iPhone - Why does the documentation say UIImageView is NSCoding compliant?
View objects support NSCoding because the view loading system uses it to load objects from Nib files. But the UINib class does some additional work that NSKeyedArchiver does not.
If you just want to store state between launches, it's better to store data and not view state. Especially if you ever plan to update your app and make any changes whatsoever to the view layout. (This is the motivation behind Model/View/Controller separation.)