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

crash info

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

how do it if use kvc ?

loseDream
  • 87
  • 1
  • 13

1 Answers1

1

Observe that you can't set the title label directly:

mybutton.titleLabel = UILabel(...)

The reason: it's a read-only property. You have no business trying to substitute a different label inside the button!

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Yes, it can, if the property supports KVC. But you are trying to write to a read only property. KVC doesn’t let you magically do that. – matt Sep 08 '18 at 21:26