ios - UIButton set titleLabel setValue(newLabel, forKeyPath: "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怎么办?
解决方案
您应该使用setTitle
方法来设置状态的按钮标题。
button.setTitle("Your button title here", for: .normal)
setValue(_:forKeyPath:)是类的一个方法,NSObject
它UIButton
是一个子类。不推荐使用 KVO。阅读此线程以获取更多信息。