If you are new to iOS programming, one design pattern that you are going to need to quickly learn is delegation: Delegates are an object oriented versions of callbacks (or that is how I like to think about them anyways). Whereas a callback is a function which is defined before an event, and is called every time an event occurs, a delegate is an object that acts on behalf of, or in coordination with, another object when that object encounters an event in a program.

The easiest way to explain this is to look at a simple example in javascript and in then in objective-c:

Lets say you want to write a very simple key logger, which dumps any key strokes to standard out.

In javascript, this can be done very easily utilizing jQuery:

$("body").on("keyup", function(e){

In this example, the callback function(e) will be called anytime a keyboard key is pressed. (Yes, I know it only works if you are in a browser, but I am trying to write a trival example)

In objective C, the same thing can be accomplished through delegation:

#import <UIKit/UIKit.h>

@interface SomeClass : UIViewController <UITextViewDelegate, UITextFieldDelegate>

@property (nonatomic, strong, readonly) SAMTextField* someTextField;


The first thing to notice here is SomeClass is conforming to the UITextFieldDelegate and UITextViewDelegate protocols. Also notice this view contains a single text field, a SAMTextField to be exact.

 1 @implementation SomeClass
 3 -(SAMTextField*) firstName {
 4     if(!_someTextField){
 5         _someTextField = [[SAMTextField alloc]
 6         initWithFrame:CGRectMake(10.0f, 00.0f, [[self class] textFieldWidth], 70.0f)];
 7         _someTextField.keyboardType = UIKeyboardTypeEmailAddress;
 8         _someTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
 9         _someTextField.autocorrectionType = UITextAutocorrectionTypeNo;
10         _someTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
11         _someTextField.returnKeyType = UIReturnKeyNext;
12         _someTextField.delegate = self;
13         _someTextField.placeholder = @"First Name";
14         _someTextField.borderStyle=UITextBorderStyleNone;
15     }
16     return _someTextField;
17 }
19 -(BOOL)textField:(UITextField *)textField
20 shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
21 {
22     if(textField == self.firstName ){
23         NSString* currentText = self.firstName.text;
24         NSLog(@"%@",self.someTextField.text)
25     }
26     return YES;
27 }
29 @end

Now let’s see how delegation works. When I am defining the text field, you will see on line 12 that I am setting my class SomeClass to be the delegate of the text field. This is me telling SAMTextField to send any protocol message from UITextFieldDelegate and UITextViewDelegate to the instance of SomeClass.

Finally, I define instance methods of any of the protocol messages I want to receive data from. In this case, I define shouldChangeCharactersInRange, which is a part of UITextFieldDelete

From the documentation:

The text field calls this method whenever the user types a new
character in the text field or deletes an existing character.