swift Delegate and Delegation Pattern for Beginners.
what is the delegate and how to implement in my App
- you can download the snippet of code in real world Application Here .
Delegation meaning : it allows one object to perform specific task ‘object acting as delegate’ on behalf of another one ‘object needing a delegate’ .
let’s dive into the real world production to get well understand
- imagine that you are building todo list, and screen A it has it is own tableView to present the todo list , which you will be added by screen B.
- in Screen A you have a segue to screen B “Show Transition” (by button +). in Screen B you’ve added todo item, which you need to add and then “(press done button)”. so that you need to present the item from screen B to add in screen A and present it
you can accomplish this target by delegate pattern , to make screen A is ‘a delegate’ of screen B to present what you wrote in screen B, because the idea behind the delegation is that allows one object (Screen A) to help another object (Screen B) for specific task that B can’t do itself . such as (Screen B) can’t present the todo item without helping (Screen A)
so for now Screen B doesn’t know anything about screen A and doesn’t care if screen A has been changed so the principle where screen B is independent of screen A and it is called loose coupling
so let’s get start doing this example by coding
- Open Xcode and click on “ Get start with a playground ” and follow this tutorial step by step to get understand well
there are Five steps for setting up the delegate pattern between two objects where for this example “Object A “ is a delegate for “object B” , and object B will send message back to object A
1- Define a delegate protocol for object B “ScreenB”
the most conventional in delegate pattern to write protocol syntax with the class name ”Screen B” followed by “Delegate” keyword and confirm to “AnyObject or class” this keyword identifies that we want the delegate protocol to limited to class type only “ reference type “
2- Give object B an optional delegate variable
this variable delegate is nothing more than a reference to some object that implements the methods of the protocol, that can send message to a delegate variable without knowing what the object really is .
delegate variable usually declared as being weak to avoid the retain cycle between objects plus it can be nil so it’s an optional
3- leads object B send message back to its delegate when something interesting happen in this case “when the user press to done button”
in our case when the user write the item and then needs to press done button to make it present in screen A so we need to make screen B send message to screen A to lead it adding this item to its items in tableView and start to present it, don’t hurry it will be clear after a moment xD : —
after adding item through textField you need to notify screen A that there’s another item you need to add to its items table so that we declare delegate variable before to be the contract between A and B
4- Make object A confirm to the delegate protocol and implemet the methods form the protocol
for the function we implement adding the item first to items array and inset new row for tableView to present new item and reload the data
5- the last step to tell object B that object A is now its delegate
to refer the object A is now the delegate of object B so when object B sends message back via delegate variable now it will send to screen A because now the screen A is the delegate of B
the proper place to declare this “The prepare(for:sender:) method” because in UIKit Framework this method is invoked when the segue from one object to another is about to be performed
Conclusion
- Our iOS app is finally completed and is ready to be run. Using delegates and protocols keeps the overall process simple yet efficient.
- you can download the snippet of code in real world Application Here .
- I hope you found this article helpful. If you did, please follow me and share this post with your friends or colleagues who might find it useful.
- If you have any questions or you need any help you can visit