问题
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