The Delegate pattern, or Delegation, is an architectural pattern of Swift code and Apple libraries that are frequently used in iOS development. It allows one object to send messages to another object when a specific event happens.
In this blog, you will learn to create a custom Delegate in Swift.
A delegate defines "Blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality”.
The Delegate can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements. Any type that satisfies the requirements of a Delegate is said to conform to that Delegate.
In addition to specifying requirements that conforming types must implement, you can spread a delegate to appliance some of these requirements or implement additional functionality that conforming types can take advantage.
There are two types of delegates we can use: optional and required. They are pretty self interpretive but the difference is required will throw an error letting you know your class is not conforming to the protocol. Also, delegate methods are required by default so if you want it optional don’t forget that optional keyword.
Delegates are the objects which allow one object to send a message as events pass.
Select single view app and click on "Next".
For product name, use CustomProtocol and then fill out the organization name and organization identifier with your customary values. Select swift as language and choose Next.
For design, the user interfaces open Main >> Storyboard file. In that, add one label and one button in first view controller.
Your storyboard should look like this :
Now, add one more view controller in Storyboard, then add one TextField and one button in second view controller.
Your storyboard should look like this :
Create a delegate protocol that defines the duty of the delegate.
Create a delegate property in the delegating class to keep path of the delegate.
Adopt the delegate methods in the delegate class.
Call the delegate from the delegating object.
In this Demo, we are passing data back using a delegate.
For example, data of next view controller passing to previous ViewController through delegate.
Now, create protocol outside the class in Second ViewController class file.
Now, add your protocol name and requirements with your function and variables, which you want to use with your protocols. I have created one protocol with the name SendDataDelegate.
Protocol Declaration in Second ViewController class file:
Now, Create UIOutlet of UITextField:
Now, Create UIAction of SendName Button :
After that, call protocol delegate method when user clicks on SendName button.
Now, put the following code in action of SendName button:
Now, add the SendDataDelegate method to use passing data in First View controller class file.
Now, Create UIOutlet of UILabel.
Here, label Display passing data of Second ViewController.
Now, create UIAction of GetName button in first ViewController class file.
Notice that, you must have to set an object of Second ViewController class delegate to Self.
Now, put the following code in action of GetName Button:
After that, Implement the following method in First ViewController Class File.
Here’s an output of second ViewController:
Now, you can see passing data of Second ViewController in First ViewController.
Here’s an output of first ViewController:
The Delegation will remain important for the iOS SDKs. Therefore, you need to learn this process for iOS application development.
It may be a little difficult for you to learn, but Delegation plays an important role in a good app architecture and makes people stick to this function. After reading this blog, you have got a clear perspective of how custom Delegate works and why you should use it.
CTO of Vasundhara Infotech, a leading Software development company in the USA. His technological interests has helped the company in making useful decisions.
Sign Up to our newsletter to get latest updates staight in your inbox.
Vasundhara respects your privancy. No Spam!
Sign Up to our newsletter to get latest updates staight in your inbox.
Vasundhara respects your privancy. No Spam!