UIButton set titleLabel setValue(newLabel, forKeyPath: "titleLabel") [英] UIButton set titleLabel setValue(newLabel, forKeyPath: "titleLabel")

查看:31
本文介绍了UIButton set titleLabel setValue(newLabel, forKeyPath: "titleLabel")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

崩溃信息

setValue:forUndefinedKey:]:该类不符合键 titleLabel 的键值编码

如果使用kvc怎么办?

how do it if use kvc ?

推荐答案

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

You should use setTitle method to set button title for states.

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

setValue(_:forKeyPath:) 是来自NSObject 类,UIButton 是其子类.不推荐使用 KVO.阅读这个线程 了解更多信息.

setValue(_:forKeyPath:) is a method from NSObject class which UIButton is a subclass of. It is not recommended to use KVO. Read this thread for more information.

这篇关于UIButton set titleLabel setValue(newLabel, forKeyPath: "titleLabel")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆