子类化 UILabel 存储在序列化视图中,不存储自定义变量

发布于 12-22 00:52 字数 576 浏览 3 评论 0原文

因此,我有一个正在序列化并存储在文件中的视图。在该视图中,有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东走西顾2024-12-29 00:52:53

您缺少对 [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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文