简体   繁体   English

用户定义的运行时属性可访问性

[英]User Defined Runtime Attributes accessibility

我在IB中为我的自定义UIView创建了一个用户定义的运行时属性,有趣的是,我可以在touchesBegan:withEvent:方法中访问它,但不能在initWithCoder中访问它:你知道为什么吗?

如果您需要在设置视图时访问值而不是在initWithCode:中处理它initWithCode:使用awakeFromNib

It is because the views from IB are finished loading after initWithCoder . 这是因为IB的视图在initWithCoder之后完成加载。 The following will help you understand the process. 以下内容将帮助您了解该过程。 It is explained for UIViewControllers , but the concept is the same (from the ViewController Programming Guide ): 它是针对UIViewControllers解释的,但概念是相同的(来自ViewController编程指南 ):

When you create a view controller in a storyboard, the attributes you configure in Interface Builder are serialized into an archive. 在故事板中创建视图控制器时,您在Interface Builder中配置的属性将序列化为存档。 Later, when the view controller is instantiated, this archive is loaded into memory and processed. 稍后,当实例化视图控制器时,此存档将加载到内存中并进行处理。 The result is a set of objects whose attributes match those you set in Interface Builder. 结果是一组对象,其属性与您在Interface Builder中设置的属性相匹配。 The archive is loaded by calling the view controller's initWithCoder: method. 通过调用视图控制器的initWithCoder:方法加载存档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM