Delegation

You have already seen the Target-Action pattern. This is one form of callbacks that is used by UIKit: When a button is tapped, it sends its action message to its target. This typically triggers code that you have written.

A button’s life is relatively simple. For objects with more complex lives, like a text field, Apple uses the delegation pattern. You introduce the text field to one of your objects: This is your delegate, when anything interesting happens in your life, send a message to him. The text field keeps a pointer to its delegate. Many of the message it sends to its delegates are informative: OK, I am done editing!. Here are some of those:

-​ ​(​v​o​i​d​)​t​e​x​t​F​i​e​l​d​D​i​d​E​n​d​E​d​i​t​i​n​g​:​(​U​I​T​e​x​t​F​i​e​l​d​ ...

Get iOS Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.