siege_Perilous
siege_Perilous

Reputation: 1109

Notification Center vs. Delegation in iOS SDK

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?

Upvotes: 4

Views: 3255

Answers (1)

Jeremy H
Jeremy H

Reputation: 452

Delegation allows you to execute methods (and optionally pass parameters) "backward." For example, if you have one class that references another class (parent and child), the child can fire back to the parent without referencing the parent.

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.

As for relations, delegation is a 1 to 1 relationship while Notification Center is a 1 to 1/many relationship.

Upvotes: 6

Related Questions