What's the nicest way to do observer/observable in objective-c (iphone version)

前端 未结 3 1631
醉话见心
醉话见心 2020-12-12 23:25

I\'m used to coding Java Swing UIs, and in those if you have some properties that change, and you want your UI to update, you would implement the observer/observable pattern

相关标签:
3条回答
  • 2020-12-12 23:32

    That's not generally the way that it's done. Take a look at the discussion here, in particular the link to the Apple documentation.

    If you still want to do it the way you say you do, it's not particularly hard to implement something like bindings "by hand". You'd just create a "binding" object that knows how to subscribe to changes, and connects to a property of a view.

    To actually answer how it's done - normally, you have a controller object that monitors the state of the model (acting something like an Observer), and updates the view object(s) as necessary.

    0 讨论(0)
  • 2020-12-12 23:50

    I also found that you can do:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_handleWhateverChange) name:@"whateverChange" object:nil];
    

    To register for change events, and

    [[NSNotificationCenter defaultCenter] postNotificationName:@"whateverChange" object:nil];
    

    To fire them. I might be a N00b but I just couldn't get the observer for key path thing to work for me.

    0 讨论(0)
  • 2020-12-12 23:53

    There are two built-in ways of doing observation in Cocoa: Key-Value Observing and notifications. In neither system do you need to maintain or notify a collection of observers yourself; the framework will handle that for you.

    Key-Value Observing (KVO) lets you observe a property of an object — including even a property that represents a collection — and be notified of changes to that property. You just need to send the object -addObserver:forKeyPath:options:context: passing the object you want to receive updates, the key path of the property (relative to the receiver) for which you want to receive updates, and the types of updates you want to receive. (There are similar methods you can use if you want to observe a property representing a collection.)

    Notifications are older and heavier-weight. You register with an NSNotificationCenter — usually the default center — an object and selector pair to be passed a notification when an event occurs. The notification object itself can contain arbitrary data via its userInfo property, and you can choose to observe all notifications of a specific name rather than those that apply to a particular object.

    Which should you use in any particular case? In general, if you care about changes to a specific property of a specific object, use Key-Value Observing. That's what it's designed for and it's intentionally lightweight. (Among other uses, it is the foundation on which Cocoa Bindings are built.) If you care about a change in state that isn't represented by a property, then notifications are more appropriate.

    For example, to stay in sync when the user changes the name of a model object, I'd use KVO. To know when an entire object graph was saved, I'd use notifications.

    0 讨论(0)
提交回复
热议问题