Pass Data Between Seque and Unwind Seque in Swift 5.2
Set up Project in Xcode
Create a new Xcode project, make it a single view app, set the language to Swift and select the Storyboard as the User Interface.
Once your project is created, implement the following changes:
- Create three new view controllers in the Storyboard (I’m going to call them View Controller A, B, and C.)
- Embed them in a navigation controller (Editor → Embed In → Navigation Controller)
- Create a show segue from View Controller First to Second and from Second to Third. Give each a unique identifier.
- Add a button to each view controller. In A and B, this is will trigger our show segue and in C this will trigger our unwind segue.
Once you’re done with this, this is how your storyboard should look like:
Create Seque from First to Second and Pass Data
class ViewController: UIViewController {
var newchracter = “varun”
var fullname = “”
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == “second”) {
let vc = segue.destination as! SecondView
vc.verificationId = newchracter
}
}
Next Step Create Seque from Second to Third and Pass Data
//
// ViewController.swift
// Unwindseque
//
// Created by APPLEMACBOOK PRO A1502 on 11/10/20.
//
import UIKit
class SecondView: UIViewController {
var verificationId = “”
override func viewDidLoad() {
super.viewDidLoad()
// print(verificationId)
// Do any additional setup after loading the view.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == “third”){
let vc = segue.destination as? Third
vc!.camera = verificationId
}
}
}
Next Final Step Create Unwind Seque
import UIKit
class Third: UIViewController {
var camera = “”
override func viewDidLoad() {
super.viewDidLoad()
print(camera)
// Do any additional setup after loading the view.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == “first”) {
let vc = segue.destination as! ViewController
vc.fullname = “hello”
}
}
@IBAction func backToATapped(_ sender: UIStoryboardSegue) {
performSegue(withIdentifier: “first”, sender: self)
}
}
Main Portion is to get Data in FirstViewController .
In First Controller we implement
@IBAction func unwind( _ seg: UIStoryboardSegue) {
}
and Here we get Data like this
@IBAction func unwind( _ seg: UIStoryboardSegue) {
print(seg.destination)
guard let c = seg.destination as? ViewController
//
else {
return
}
print(c.fullname)
}