Swift Passing data back to controller

April 14, 2020

This is the Storyboad for this example and we going o pass data from VC2 to VC1

We have two View Controllers named VC1 and VC2 and corresponding classes are VC1.swift and VC2.swift

Create a segue from button from Go to VC2 to VC2 View Controller. Name its identifier as seguevc2

In VC2.swift

First we will start with VC2

You have three UI elements here UILabel,UITextField and UIButton. You can create the Outlet and Action for them

@IBAction func btnUpdate(_ sender: Any) {}
 
@IBOutlet weak var txtPrice: UITextField!
    

Very top of the VC2.swift class please create the delegate protocol

protocol UpdatePriceDelegateProtocol {
    func updatePrice(newPrice: String)
}

Now you can define the delegate variable

var delegate: PriceUpdateDelegateProtocol? = nil

Now you can call the method of the delegate and update the price

self.delegate?.updatePrice(newPrice: self.txtPrice.text!)

This is the completed code of the VC2.swift

import UIKit

protocol UpdatePriceDelegateProtocol {
    func updatePrice(myData: String)
}

class VC2: UIViewController {
    
    var delegate: UpdatePriceDelegateProtocol? = nil
    @IBAction func btnUpdate(_ sender: Any) {
        
        if self.delegate != nil && self.txtPrice.text != nil {
            self.delegate?.updatePrice(myData: self.txtPrice.text!)
            dismiss(animated: true, completion: nil)
        }
    }
    @IBOutlet weak var txtPrice: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad(
     }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

VC1.swift

First you conform to UpdatePriceDelegateProtocol and then implement the delegate function

class VC1: UIViewController,UpdatePriceDelegateProtocol {
    
    func updatePrice(myData: String){
        
    }
    .......
}

Next thing is to write the prepare function to work with segue. In this function we can assign VC1 controller to the delegate variable of the VC2

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "seguevc2" {
            let secondVC: VC2 = segue.destination as! VC2
            secondVC.delegate = self
        }
    }

This is the completed code of the VC2.swift


import UIKit
protocol UpdatePriceDelegateProtocol {
    func updatePrice(myData: String)
}

class VC2: UIViewController {
    
    var delegate: UpdatePriceDelegateProtocol? = nil
    @IBAction func btnUpdate(_ sender: Any) {
        
        if self.delegate != nil && self.txtPrice.text != nil {
            self.delegate?.updatePrice(myData: self.txtPrice.text!)
            dismiss(animated: true, completion: nil)
        }
    }
 
    @IBOutlet weak var txtPrice: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
}

You can Download Source Code for this project and run the project to see how it work. You can browse the source code and understand the complete implementation of the project