10

UIButton has a state property, which appears to be KVO compliant by all accounts and there is no documentation to indicate otherwise. However, when I added an observer to a UIButton's state property, the observer callback was never invoked. How come?

Evil Nodoer
  • 1,927
  • 3
  • 23
  • 32

1 Answers1

23

If you look at the documentation of UIControl, the state property is marked: synthesized from other flags.

I guess this is why changes to this property are not KVO compliant.

However, you can simply register and observer for the values you need - highlighted, selected, enabled. These properties are KVO compliant and you will get the observer callback when they change.

adamsiton
  • 3,642
  • 32
  • 34
  • 4
    Thank you. I am currently KVO-ing on other properties. In general, I feel there is a lack of documentation on what properties are KVO compliant and what are not in Cocoa; do you feel the same way? – Evil Nodoer Sep 25 '11 at 15:17
  • 3
    I tried observing `selected` on `UIButton`, but my observer isn't called. Switched to `highlighted`, and it works. Using Swift 2. – Nicolas Miari Oct 14 '15 at 12:00
  • My button acts like a switch, so in the end I decided to listen to `highlighted`. It gets called twice (on highlighting and de-highlighting), so I ignore it if `highlighted` is `true`, and then proceed to inspect the `selected` state of the button. – Nicolas Miari Oct 14 '15 at 12:11