Updated 6 June 2019
So let’s assume you have two view controllers: FirstViewController and SecondViewController. The user navigates from FirstViewController to the SecondViewController. SecondViewController will process some business logic and the result of that business logic will need to be sent back to the FirstViewContoller when the user navigates back.
Here I’m using the call back for sending data back from SecondViewController to FirstViewController.
-> Callbacks are very important when we are doing any asynchronous task in the background and need to change the UI based on the response.
First, we make the call back in SecondViewController.
1 |
var callBack: ((_ id: String, _ name: String, _ age: Int)-> Void)? |
here this callback returns void means nothing.
We will Navigate from FirstViewController to the SecondViewController. here we are using below code to push from FirstViewController to the SecondViewController.
1 2 3 4 5 6 7 8 9 10 11 12 |
//function for navigate from FirstViewController to SecondViewController @IBAction func pushToSecond(_ sender: Any) { if let vc = storyboard?.instantiateViewController(withIdentifier: "SecondViewController")as? SecondViewController { //callBack block will execute when user back from SecondViewController. vc.callBack = { (id: String,name: String,age: Int) in print(id,name,age) } self.navigationController?.pushViewController(vc, animated: true) } } |
Now we will call callBack from SecondViewController when the user press on the back button.
1 2 3 4 5 6 7 8 9 |
//back from SecondViewController to FirstViewController @IBAction func BackToFirstWitData(_ sender: Any) { //here we call the callBack on back press action callBack?("1","Test",22) self.navigationController?.popViewController(animated: true) } |
In this step, the callback block will execute and print the data.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@IBAction func pushToSecond(_ sender: Any) { if let vc = storyboard?.instantiateViewController(withIdentifier: "SecondViewController")as? SecondViewController { //callBack block will execute when user back from SecondViewController. vc.callBack = { (id: String,name: String,age: Int) in // In this block you can excute anything print(id,name,age) } self.navigationController?.pushViewController(vc, animated: true) } } OutPut:- 1,Test,22 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
//FirstViewController import UIKit class FirstViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func viewWillAppear(_ animated: Bool) { } @IBAction func pushToSecond(_ sender: Any) { if let vc = storyboard?.instantiateViewController(withIdentifier: "SecondViewController")as? SecondViewController { vc.callBack = { (id: String,name: String,age: Int) in print(id,name,age) } self.navigationController?.pushViewController(vc, animated: true) } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
//SecondViewController import UIKit class SecondViewController: UIViewController { var callBack: ((_ id: String, _ name: String, _ age: Int)-> Void)? @IBAction func BackToFirstWitData(_ sender: Any) { callBack?("1","Test",22) self.navigationController?.popViewController(animated: true) } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } } |
So pls follow the above step and And if you have any issue or suggestion you can leave your message in the comment section I will try to solve this.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
3 comments