Here is an example to explore Key Value Observing. We have a popup whose Content is bound to the variable L in the AppDelegate and its selectedIndex is bound to the variable i. The Text Field is also bound to the variable i in the AppDelegate. There are three buttons connected to actions of the same names, which change the value of i in different ways: (1) call a non-KVO-compliant method doit(), (2) use an accessor set method for the i variable, or (3) call willChangeValueForKey_("i"), call the doit() method, and call self.didChangeValueForKey_("i"). The Observer class is registered to observe the Key Path "i."
2008-07-27 21:54:56.189 PyKVO[2873:10b] go1 |
The action go1_ (doit()) results in notification to the Observer, as does use of go2_ (setI_()). But go2_ results in duplicate notifications, as does calling go3_, which I thought was the correct way for a non-compliant change. Changing the text field in the window sends duplicate notifications as well. So apparently, use of the setter or editing the textField sends both "willChange" and "didChange" notifications, whil changing the variable directly sends only one, probably "didChange").
Here is the code:
class PyKVOAppDelegate(NSObject): |