问题描述
我想知道,插座上的didSet何时/如何触发?
I'm wondering, when/how does the didSet on an outlet actually trigger?
例如:
@IBOutlet weak var modifyButton: UIButton! {
didSet {
modifyButton.layer.cornerRadius = 9
}
}
推荐答案
刚刚初始化类时,插座属性初始化为nil.当从nib初始化对象时,它们将具有稍后的值.可以确定所有正确配置的插座属性的值为nil的第一步是viewDidLoad
.因此,将在viewDidLoad
之前调用此属性的didSet
观察者.因为所有这些,您需要注意插座属性:例如,如果尝试使用prepareForSegue
方法
Outlet properties initialized as nil when class just initialized. They will have values later, when objects will be initialized from nib. First step, when you can be sure that all properly configured outlet properties have nonnil values is viewDidLoad
. So, didSet
observer on this properties will be called just before viewDidLoad
. Because all of this you need to be careful with outlet properties: for example you will receive nil if you will try access them in prepareForSegue
method
这篇关于何时/如何-出口-> didSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!