子类化 UILabel 存储在序列化视图中,不存储自定义变量
因此,我有一个正在序列化并存储在文件中的视图。在该视图中,有 n 个 UILabels 子类,唯一的区别是 live 属性。我在 UILabel 子类中有 initwithcoder 和encodewithcoder,但我仍然无法在标签中获取自定义变量。我已经包含了我的子类的方法和以下内容。任何帮助表示赞赏。
自定义 UILabel:
- (id) initWithCoder:(NSCoder *)decoder
{
self = [super initWithCoder:decoder];
if (self != nil) {
self.live = [decoder decodeBoolForKey:@"live"];
}
return self;
}
-(void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeBool:self.live forKey:@"live"];
}
因为我只是取消归档视图,其中包含其中的标签,所以我假设 ios 不会取消归档自定义标签?
谢谢
So, I have a view that is being serialized and stored in a file. Within that view, is n subclassed UILabels, with the only difference being a live property. I have the initwithcoder and encodewithcoder within the UILabel subclass, but I am still unable to get the custom variable within the label. I have included my subclass' methods and the below. Any help is appreciated.
Custom UILabel:
- (id) initWithCoder:(NSCoder *)decoder
{
self = [super initWithCoder:decoder];
if (self != nil) {
self.live = [decoder decodeBoolForKey:@"live"];
}
return self;
}
-(void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeBool:self.live forKey:@"live"];
}
Because I am only unarchiving the view, which contains the labels within, I assume that ios does not unarchive the custom labels?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

您缺少对
[superencodeWithCoder:aCoder]
的调用。这将导致错误序列化的对象,我不确定后果是什么,但你似乎已经找到了其中之一!You are missing a call to
[super encodeWithCoder:aCoder]
. This will result in an incorrectly serialised object, I'm not sure what the consequences are, but you seem to have found one of them!