在 didSet 中初始化属性
@IBOutlet weak var title: UILabel! {
didSet {
label.textColor = UIColor.redColor()
label.font = UIFont.systemFontOfSize(20)
label.backgroundColor = UIColor.blueColor()
}
}
也可以设置一个值并初始化它:
private var loginButton = UIButton() {
didSet(oldValue) {
loginButton.addTarget(self, action: #selector(LoginController.didClickLogin), forControlEvents: .TouchUpInside)
}
}