0

Share

APP DEVELOPMENT

How To Create Custom Delegate In iOS Swift?

  • Home
  • Blogs
  • How To Create Custom Delegate In iOS Swift?

Delegate in Swift is a controller object with a defined interface that can control or change the behavior of another object. It communicates in a decoupled way.

Somish Kakadiya

July 15, 2022

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. 

Custom Delegate

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.

Let’s create a single view application with custom protocol:




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.




Storyboard User Interface:

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 :




Basic Steps To Use Delegation:

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.

Implementation Of Custom Delegate:

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:

Create UIOutlet Of UITextField:

Now, Create UIOutlet of UITextField:

Create UIAction:

Now, Create UIAction of SendName Button :

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

Create UIOutlet Of UILabel:

Now, add the SendDataDelegate method to use passing data in First View controller class file.

Now, Create UIOutlet of UILabel.

Create UIAction Of GetName Button:

Here, label Display passing data of Second ViewController.

​​​​​​​Now, create UIAction of GetName button in first ViewController class file.

Action Of GetName Button:

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:

Implement Method:

After that, Implement the following method in First ViewController Class File.

Output:

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:




Conclusion:

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.

img

Somish Kakadiya

CTO of Vasundhara Infotech, a leading Software development company in the USA. His technological interests has helped the company in making useful decisions.

message

Have a project in mind? Drop a message to Bansi Pipaliya & start the discussion!

Get a Newsletter

Sign Up to our newsletter to get latest updates staight in your inbox.

Vasundhara respects your privancy. No Spam!

Get a Newsletter

Sign Up to our newsletter to get latest updates staight in your inbox.

Vasundhara respects your privancy. No Spam!

message

Have a project in mind? Drop a message to Bansi Pipaliya & start the discussion!

Latest 13 Web Development Trends To Expect In 2022
April 11, 2022 Category : company news

Revealing Vasundhara’s New Identity

Read More
Latest 13 Web Development Trends To Expect In 2022
May 17, 2022 Category : web development

Latest 13 Web Development Trends To Expect In 2022

Read More
Leave a Comment