为什么 UIButton 不能使用 KVC 改变 titleLabel

Why UIButton can not use KVC to change titleLabel

let newLabel = UILbael()
let button = UIButton()
button.setValue(newLabel, forKeyPath: "titleLabel")

崩溃信息

setValue:forUndefinedKey:]: this class is not key valuecoding-compliant for the key titleLabel

如果使用kvc怎么办?

注意不能直接设置标题标签:

mybutton.titleLabel = UILabel(...)

原因:它是 read-only 属性。您没有必要尝试替换按钮内的不同标签!