swift Delegate and Delegation Pattern for Beginners.

  • 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”

think of delegate protocol as a contract between screen B and screen A

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 : —

NOTE: it’s customary for delegate method to have a reference to its owner object for the first parameter, it’s not required but it still a good idea to distinguish if you have another screen B in project, for example if an object is a delegate for more than one tableView so in this case you need to distinguish between two those tableView, to allow this the TableView delegate methods have the first parameter for UITableView object itself.

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

Using prepare-for-segue allows you to pass data to the new view controller before it is displayed.

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

1- Facebook account

2- twitter account

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ahmed Amin Hassan

Ahmed Amin Hassan

I've graduated from electrical power engineering 🤓 but that's not what I dreamed so after diving into mobile Apps I found myself, to be an 'iOS Developer'💻📲