Notification Center vs. Delegation in iOS SDK

£可爱£侵袭症+ 提交于 2020-01-14 10:42:50

问题


Why did Apple choose to use delegation for some communication amongst SDK objects and to post notifications to Notification Center for others?

In particular, I'm thinking about the set of keyboard appearance notifications that come from UIWindow.

Is it because the Notification Center system means that more than one object can use the keyboard appearance action as a trigger to change its state, whereas only one object would have been able to act with a delegate implementation?


回答1:


Delegation allows you to execute methods (and pass parameters) from one class back into another. This allows for a method to be fired even when that class is not imported. Delegation allows for multiple view controllers (or otherwise classes) to fire methods back in a single view controller.

Notification Center, on the other hand, listens and waits until it hears the message it is waiting for. This allows for multiple listeners in multiple view controllers to wait and listen for a given message.

You could say that delegation is a 1/many to 1 relationship while Notification Center is a 1/many to 1/many relationship.



来源:https://stackoverflow.com/questions/19215346/notification-center-vs-delegation-in-ios-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!