The delegate patterns have been there for a very long period on the Apple platform. Delegation is mainly used for handling table view events using UITableViewDelegate, to modify cache. The core purpose of a delegate is to report back to the owner in a decoupled way.
What exactly is Delegate?
Delegates are a design pattern that allows one object to send messages to another object when a specific event happens.
Consider an Object A calls an Object B for action and A should know that B has completed the action. This can be easily achieved with delegates
How to create a Delegate?
I’m going to show you how to create a custom delegate that passes between classes.
Consider having two classes ViewControllerA and ViewControllerB. A has a UILabel and a Button and B has TextField & a Button. Clicking on a button in A will navigate to B. When the user enters a data in the TextField,it will return back to A when the user clicks the Button in B.
Step 1 : Create a Protocol in ViewControllerB
This is the first step towards creating a delegate. You can implement as many functions you want depending on the requirement. In our case, we need only one function that accepts the string.
Step 2 : Create a delegate property in ViewControllerB
weak var delegate : ViewControllerBDelegate ?
We must create delegate property for the class and give protocol ViewControllerBDelegate as the type and it should be optional. And you should add the weak keyword to avoid memory leaks.
Step 3: Add Delegate method call in tap event in ViewControllerB
This should be added in tap event of the button which takes back to ViewControllerA.
That contains a text which is from the TextField in ViewControllerB.
Step 4 : Import delegate in ViewControllerA
Now ViewControllerA has taken ViewControllerBDelegate protocol but the compiler will throw an error saying “Type ViewControllerA does not conform to protocol ViewControllerBDelegate__” this means you didn’t use the protocol in ViewControllerA.
We must use all the functions from the delegate. Adding the function to class ViewControllerA that are added in ViewControllerBDelegate will remove this error.
Step 5 : Assign ViewControllerB to ViewControllerA
If you are using segue for navigation, bind it through the prepareforsegue method.
if you are using delegate within a ViewController (Like data transition between ViewController and UIView). Add the below code in viewDidLoad of ViewControllerB
Step 6: Finally use the method of the ViewControllerBDelegate
Every time you click on the Button in ViewControllerB, you will get the textData String from the textFromTextField function.
Conclusion
Delegation is an important design pattern to understand if you want to become an iOS developer. Delegation touches on many principles of good app architecture and proves its uselessness even though it is an old design pattern.
Subscribe to our newsletter
Get the latest updates from our team delivered directly to your inbox.
Related Posts
3 days with iOS - the Android user's perspective
The title is very evident, but it's always difficult to convince an Android fanatic to use iOS on an iPhone. Adding more fuel to the Android vs iOS debate, we got our Mobile Application Developer to test iOS and see how it fares in comparison with Android - and here are his thoughts.
Android O
The new and upgraded Android, Version 8.0 was launched on Monday as the Solar Eclipse was under way and we played around a bit with the new version - and this is what we found.
How we Automate Android App Distribution with Fastlane
Last time we saw how to deploy iOS apps with FastLane, now let us see how to use FastLane to push Android apps to Google Play Store.