Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data with unwind segue

I created two view controllers. I created a segue from the first to the second to pass data. Now I want to pass data from the second view controller to the first one. I went through many similar questions and I'm not able to implement those as I lack the knowledge on how unwinding works.

ViewController.swift

class ViewController: UIViewController {        var dataRecieved: String?     @IBOutlet weak var labelOne: UILabel!     @IBAction func buttonOne(sender: UIButton)     {         performSegueWithIdentifier("viewNext", sender: self)     }     override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!)     {          var svc: viewControllerB = segue.destinationViewController as! viewControllerB         svc.dataPassed = labelOne.text     } } 

This will pass the data to dataPassed in view controller "viewControllerB". Say, now I want to pass some data from viewControllerB to dataRecieved in ViewController. How can I do this with only unwind segue and not by using delegate. I'm quite new to swift, would appreciate a detailed explanation.

like image 544
ebby94 Avatar asked Feb 10 '16 11:02

ebby94


People also ask

How do you pass data in unwind segue?

You create the unwind segue as per usual - drag to the exit icon in the scene. Now in the object inspector on the left you will see the unwind segue listed below the view controller, first responder and exit icons. You can click on the unwind segue and give it an identifier in the inspector on the right.

How do you use segue in swift 5?

method will be called on your view controller, at which point you can configure your destination view controller by setting some properties. When you tap the enter last name button, it will trigger the segue function and also show the FirstViewController. swift.


1 Answers

Øyvind Hauge beat me to the same solution method, but as I had already started with a more detailed answer, I'll add it as well.


Let's say your two view controllers are named as follows:

  • Master/entry point: ViewController (vcA)
  • Secondary view: ViewControllerB (vcB)

You set up the segue from (vcA) -> (vcB) as you have done in you example

/* in ViewController.swift */     // ...  // segue ViewController -> ViewControllerB override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!) {     if segue.identifier == "viewNext" {         let viewControllerB = segue.destinationViewController as! ViewControllerB         viewControllerB.dataPassed = labelOne.text     } } 

The somewhat tricky step next is that, using this method, the segue used for passing data back from (vcB) to (vcA) is also added to the source of (vcA), as an @IBAction method (rather than, as could possibly be expected, added to the source of (vcB)).

/* in ViewController.swift */     // ...  // segue ViewControllerB -> ViewController @IBAction func unwindToThisView(sender: UIStoryboardSegue) {     if let sourceViewController = sender.sourceViewController as? ViewControllerB {         dataRecieved = sourceViewController.dataPassed     } } 

You thereafter connect say, a button in (vcB) to this unwind action in (vcA) via the manual Exit segue in (vcB):

enter image description here

Below follows a complete example of passing text from (vcA) to (vcB); (possibly) modifying that text via an UITextField, finally returning the (possibly) modified text to (vcA).


(vcA) source:

/* ViewController.swift: Initial view controller */ import UIKit  class ViewController: UIViewController {      var dataRecieved: String? {         willSet {             labelOne.text = newValue         }     }     @IBOutlet weak var labelOne: UILabel!      @IBAction func buttonOne(sender: UIButton) {         performSegueWithIdentifier("viewNext", sender: self)     }      // set default labelOne text     override func viewDidLoad() {         super.viewDidLoad()          labelOne.text = "Default passed data"     }      // segue ViewController -> ViewControllerB     override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!)     {         if segue.identifier == "viewNext" {             let viewControllerB = segue.destinationViewController as! ViewControllerB             viewControllerB.dataPassed = labelOne.text         }     }      // segue ViewControllerB -> ViewController     @IBAction func unwindToThisView(sender: UIStoryboardSegue) {         if let sourceViewController = sender.sourceViewController as? ViewControllerB {             dataRecieved = sourceViewController.dataPassed         }     } } 

(vcB) source (note that the UITextFieldDelegate delegate here is only used for "locally" mutating the value of the dataPassed property, which will be returned to (vcA) and assigned to dataRecieved property of the latter)

/* ViewControllerB.swift */ import UIKit  class ViewControllerB: UIViewController, UITextFieldDelegate {      var dataPassed : String?     @IBOutlet weak var textField: UITextField!      // set default textField text to the data passed from previous view.     override func viewDidLoad() {         super.viewDidLoad()          textField.text = dataPassed          // Handle the user input in the text field through delegate callbacks         textField.delegate = self     }       // UITextFieldDelegate     func textFieldShouldReturn(textField: UITextField) -> Bool {         // User finished typing (hit return): hide the keyboard.         textField.resignFirstResponder()         return true     }      func textFieldDidEndEditing(textField: UITextField) {         dataPassed = textField.text     } } 

Example execution:

enter image description here

like image 96
dfrib Avatar answered Oct 17 '22 00:10

dfrib