Swift Passing data back to controller

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

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

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


First you conform to UpdatePriceDelegateProtocol and then implement the delegate function

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

This is the completed code of the VC2.swift

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