Clicky

iOS Dev Nugget 78 Delegates or NSNotification

.

Need to run a code review on your codebase? Hire me

In Objective C, there are 2 primary ways of informing an observing object that an event has happened and that the observing object can/should perform an action related to it. They are the delegate pattern used everywhere in the Cocoa Touch frameworks (look for properties called delegate or dataSource that uses a protocol) as well as notifications such as UIKeyboardWillShowNotification.

Sometimes either of them can be used. A good rule of thumb I defer to is if there needs to be 1 or possibly multiple observers. If it is always zero or one interested object observing for the event, and the observing object ”owns” the other object (such as when one view controller presents another, the former ”owns” the latter), you will usually want to use a delegate. If there might be multiple objects interested in updates, e.g. multiple view controllers refreshing when an object has changed, post notifications.

Note also that, if you require a response to complete the process, you'll have to use a delegate such as MFMailComposeViewControllerDelegate#mailComposeController:didFinishWithResult:error: requiring the delegate to dismiss the view controller.


Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No

.

Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter

.

View archives of past issues