【问题标题】:Why UIButton can not use KVC to change titleLabel为什么UIButton不能使用KVC来改变titleLabel
【发布时间】:2018-09-05 05:24:50
【问题描述】:
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怎么办?

【问题讨论】:

标签: ios swift uibutton key-value-coding


【解决方案1】:

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

mybutton.titleLabel = UILabel(...)

原因:它是一个只读属性。您没有资格尝试在按钮内替换不同的标签!

【讨论】:

  • 可以,如果属性支持 KVC。但是您正在尝试写入只读属性。 KVC 不会让你神奇地做到这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 2020-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多