

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Set up a simple delegate to communicate between two view controllers in iPhone
In this article, you learn about delegates and creating delegates. First of all,
What’s a delegate?
Delegate is a simple term that refers to communication between objects. It is a simple way of connecting objects and communication between them.
How does delegate work?
A delegate is created with help of protocol. A protocol is declared in class, inside which some event will happen, that should notify other classes. In protocol we write the declaration of function and it is defined inside the calling class.
How to create a delegate?
We’ll do this with help of an example project.
Steps to perform −
Create a class, name it as FirstViewController, create another class and call it SecondViewController. Create their respective View controllers in the storyboard.
Declare a protocol in SecondViewController, A protocol is declared outside any class or other objects.
protocol SecondViewControllerDelegate { func buttonPressedInVC2() }
In the SecondViewController, Create an optional object of the delegate we just created,
var delegate: SecondViewControllerDelegate?
We need to call the function declared in protocol, when some event happens in SecondViewController, let’s create an event that happens when a button is pressed in second view controller.
@IBAction func buttonTapped(_ sender: UIButton) { self.delegate?.buttonPressedInVC2() self.navigationController?.popViewController(animated: true) }
This is all required in SecondViewController, now let’s work on FirstViewController.
We need to conform/ implement the FirstViewController to SecondViewControllerDelegate, and it will automatically ask you to add protocol stubs.
extension FirstViewController: SecondViewControllerDelegate { func buttonPressedInVC2() { } }
Inside the protocol we just implemented we should write the code we want to execute when this delegate action happens.
Let’s create a label In FirstViewController and change it’s text when delegate method Is called.
extension FirstViewController: SecondViewControllerDelegate { func buttonPressedInVC2() { self.lblOne.text = "Delegate Implemented" } }
One last step left is, that the delegate object in SecondViewController is optional and would be nil until it is assigned, So In the FirstViewController we’ll assign that object while going from FirstViewController to SecondViewController.
Let’s create a button in FirstViewController for that.
@IBAction func goToNextVC(_ sender: Any) { let vc = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController vc.delegate = self self.navigationController?.pushViewController(vc, animated: true) }
And if you didn’t realize it yet, we need to embed FirstViewController in a Navigation Controller. Let’s run the app and see how it works.
- Related Questions & Answers
- Difference between Simple and Complex View in SQL
- What is the difference between Func delegate and Action delegate in C#?
- Hang Up Your iPhone with the Click of a Button
- How can I view the indexes I have set up in MySQL?
- How to declare a delegate in C#?
- How to compare two NSDates in iPhone/iOS?
- Action Delegate in C#
- How to communicate between Activity and Service in Android using Kotlin?
- How to find prime numbers between two values or up to a value in R?
- Set a border between two lines with CSS
- Difference Between View and Materialized View
- How to Show and hide a View with a slide up/down animation in android?
- Python program to communicate between parent and child process using the pipe.
- Set Menus to drop up with Bootstrap
- How to Set opacity for View in iOS?