My brother and I are starting out with making our iOS app with Swift by doing the Start Developing iOS Apps (Swift) tutorial made by Apple. In the beginning of the tutorial, it has us use a delegate to allow a button to change the text of a label to the text in a text field. We are not sure why we need to use delegates and have the classes do the data access themselves. We have done a lot of research and it seems that the only reason why delegates are used is when you need to give data back to a previous view controller. Why do you need to use delegates in a single view controller? Thank you.
2 Answers
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 delegating object is often a responder object—that is, an object inheriting from NSResponder in AppKit or UIResponder in UIKit—that is responding to a user event. The delegate is an object that is delegated control of the user interface for that event, or is at least asked to interpret the event in an application-specific manner.
Delegation methods have a conventional form. They begin with the name of the AppKit or UIKit object doing the delegating—application, window, control, and so on; this name is in lower-case and without the “NS” or “UI” prefix. Usually (but not always) this object name is followed by an auxiliary verb indicative of the temporal status of the reported event. This verb, in other words, indicates whether the event is about to occur (“Should” or “Will”) or whether it has just occurred (“Did” or “Has”). This temporal distinction helps to categorize those messages that expect a return value and those that don’t.
You must distinguish the framework available delegates from the framework class protocol/delegate you could create:
Becoming the Delegate of a Framework Class A framework class or any other class that implements delegation declares a delegate property and a protocol (usually a formal protocol). The protocol lists the required and optional methods that the delegate implements. For an instance of your class to function as the delegate of a framework object, it must do the following:
- Set your object as the delegate (by assigning it to the delegate property). You can do this programmatically or through Interface Builder.
- If the protocol is formal, declare that your class adopts the protocol in the class definition.
Example:
class myClass: NSObject,myCustomDelegate {
...
}
Also in this SO thread you can find some examples..
- Implement all required methods of the protocol and any optional methods that you want to participate in.
Apple official Source

- 1
- 1

- 34,887
- 11
- 106
- 133
-
I understand what a delegate is, I just don't understand why you need one in one view controller. It makes sense when a view controller is giving information back to a previous one because the view controller doesn't know about the previous one. However, in a single view controller all of the objects have information about each other. Why can't the object classes just do it themselves? Why do they need a delegate to do everything? Thank you. – Kyle Marieb Jun 23 '16 at 17:10
-
As explained in the first part of my answer, you could see the "framework delegates" as a interface methods to know more things about your object: UITextField have a delegate method who permit him to know when users have tapped the return button of the keyboard ( textFieldShouldReturn) so you have myTextField.delegate = self (my actual class where I've my stuff) . Why set delegate = self ? Because I want to know events happened around myTextField ;) – Alessandro Ornano Jun 23 '16 at 17:35
-
So using delegates gives a class more information about another class that would not be available without using delegates? – Kyle Marieb Jun 23 '16 at 18:21
-
Using this kind of delegates give to your custom class more information about his objects (textfields, tables..) intercepting their events, call their values updated in time real, depend by the framework class you adopting to know his delegates (UITableViewDelegate,UITextFieldDelegate...) – Alessandro Ornano Jun 23 '16 at 18:24
-
Ok thank you so much I am understanding it a lot better. Another quick question, do those methods such as textFieldShouldReturn have a lot of behind the scenes code? How does the app know what that means when it's a protocol and doesn't have any implementation? – Kyle Marieb Jun 23 '16 at 18:39
-
Because their protocols are inside the iOS framework sources, you need just to declare in your class myCustomClass: UITextFieldDelegate and in bottom you must declare textFieldShouldReturn as explained in the Apple official document because these methods you going to declare in your custom class must respect the hidden protocols inside UIKit: effective as it is simple. – Alessandro Ornano Jun 23 '16 at 18:44
-
2Awesome thank you so much. – Kyle Marieb Jun 23 '16 at 18:57
So you probably use storyboards, and all of you UIView components such a UIButtons/UITextFields and etc... are outlets, and you have access to them directly in your UIViewController.
Delegation:
Delegation is one to one connection relationship, imagine a situation where you need a build a complex layout for your view, you create a subclass of UIView and draw/layout your components there, than add this view to subivew in your UIViewController, than if you need to listen of the events in that view (Button Clicks, ScrollView offset changes and etc, you will need to create a protocol that listen those events, and tell to your UIViewController that implements that protocol witch lives in your complex UIView, because of the MVC your controller is the one who need to change views, make request and stuff, he must do the hard work, so you need to pass the data or events to your controller via that Delegate.
I'm not a teacher so i hope you understand my point.

- 144
- 1
- 9