开发者

NSNotifications being sent all across the app

Is it bad form to use NSNotifications back an开发者_如何学编程d forth all over my app?


The beauty of NSNotification is that it allows cause and effect to be decoupled. A NSNotification broadcaster doesn't care who's listening, or what they do once they get it. An NSNotification consumer doesn't care who sent the message. That's a GOOD thing.

However, there ARE times you want cause and effect to BE coupled. At that time, you want to use a direct method call between to view controllers, or Key-Value Observing, or something more concrete.

I wouldn't make a hard-and-fast rule out of it, but if you're having trouble keeping mental track of all your NSNotifications, that's probably too many.


Not per se, but it would probably be a symptom of some underlying design problem.


Typically you'd want to use notifications only when more than one object may want to be informed of the event. When there's only one interested object, use the delegate pattern. There's an excellent overview in the Cocoa Fundamentals guide under "Cocoa Design Patterns."

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜