O'Reilly logo

iOS App Development Portable Genius by Richard Wentk

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Understanding Delegates and Protocols

Earlier chapters introduced two ideas:

bullet_2c.tif Protocols. A protocol is a bundle of optional methods that can be “adopted”—or used in—any object.

bullet_2c.tif Delegates. A delegate is an object that handles optional messages. In practice, delegates typically include code that makes protocol methods do something useful.

Many frameworks use protocols and delegates, because they’re a convenient way to implement certain kinds of features.

Understanding delegates

Officially, a delegate is an object that handles a message on behalf of some other object. But this definition is misleading in practice. Objects often delegate messages to themselves. If you don’t know how delegates are used in iOS, it’s difficult to understand why.

In practice, delegate messages are usually used to add extra code that runs when something significant is about to happen, something has already happened, or iOS is asking for data and/or an object. This extra code is usually optional, and you can build a useful app without it. But it provides some extra “hooks” you can use to make your app behave in a more sophisticated and useful way.

note_2c.eps

“Something just happened” can mean “the user did something” ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required