首页 > 解决方案 > 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怎么办?

标签: iosswiftuibutton

解决方案


您应该使用setTitle方法来设置状态的按钮标题。

button.setTitle("Your button title here", for: .normal)

setValue(_:forKeyPath:)是类的一个方法,NSObjectUIButton是一个子类。不推荐使用 KVO。阅读此线程以获取更多信息。


推荐阅读