iphone - 当 View 包含带有自定义图像的 UIButton 时,UIViewControllerencodeWithCoder 失败

标签 iphone ios ipad uibutton nscoding

基本上,我的问题正是标题中所说的。当我尝试对 UIViewController 的子类进行编码时,调用 [superencodeWithCoder] 会给出 NSInvalidArgumentException。具体来说,我得到 -[UIImageencodeWithCoder:]: unrecognized selector sent to instance XxXXXXXX

View 中唯一的图像位于 UIButton 上,它也应该符合 NSCoding,并且堆栈跟踪包含对 [UIBUttonencodeWithCoder] 的调用。该按钮是使用 [UIButton buttonWithType:UIButtonTypeCustom] 以编程方式创建的,并且使用 setImage: forState: 设置图像。我真的不知道这里发生了什么事。我是否遗漏了一些明显的东西,或者 UIButton 只是不真正符合 NSCoding

最佳答案

我无法想象为什么您想要在应用程序中存档 View 对象,但您可能需要通过在 UIImage 上编写类别来添加 NSCoding 支持。

详情请参阅:iPhone - Why does the documentation say UIImageView is NSCoding compliant?

View 对象支持 NSCoding,因为 View 加载系统使用它从 Nib 文件加载对象。但是 UINib 类做了一些 NSKeyedArchiver 没有做的额外工作。

如果您只想存储启动之间的状态,最好存储数据而不是查看状态。特别是如果您计划更新应用程序并对 View 布局进行任何更改。 (这就是模型/ View / Controller 分离背后的动机。)

关于iphone - 当 View 包含带有自定义图像的 UIButton 时,UIViewControllerencodeWithCoder 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7589806/

相关文章:

iphone - 如何设置自动插入 $ 格式的小数值的文本字段值?

iphone - 如何从UICollectionView删除项目?

iphone - iOS在调用方法之前检查委托(delegate)是否存在

ios - 为什么使用 viewWithTag 进行转换时没有警告?

iOS 13 在后台关闭应用程序 : Audio Stream Issue

iphone 应用程序不工作 ipad 模拟器

iphone - 如何以特定格式以编程方式创建 pdf?

iphone - 我如何感知用户将手指按住屏幕而不移动?

iphone - iOS 4.0 中 CGContext DrawLayerAtPoint 的性能问题

java - Xcode 图像链接 ImageView 到下一个 View Controller