-3

I am new to Swift and was wondering on how to pass data from ViewController to ViewController that isn’t connected by segue. I have spend 2 hours on this and still can’t figure a way to do it.

Edit: The problem that I am confused on is How to take out all the stacked ViewController expect the root ViewController while being able to pass data back to the first ViewController from ViewController3 with 'navigationController?.popToRootViewController(animated: false)'

class ViewController: UIViewController, nameDelegate {
    @IBOutlet weak var label1: UILabel!
    @IBOutlet weak var textfield1: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        label1.text = "Name"
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let vc2 = segue.destination as! ViewController2
        vc2.middleOfTransferingName = textfield1.text!
    }

    @IBAction func buttonPressed1(_ sender: Any) {
        performSegue(withIdentifier: "go2", sender: self)
    }

    func nameThatWasEntered(name: String) {
        label1.text = name
    }
}

class ViewController2: UIViewController, nameDelegate {
    var middleOfTransferingName: String = ""

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let vc3 = segue.destination as! ViewController3
        vc3.transferedWithSegueLabelText = middleOfTransferingName
        vc3.namePassBack = self
    }

    @IBAction func buttonPressed2(_ sender: Any) {
        performSegue(withIdentifier: "go3", sender: self)
    }

    func nameThatWasEntered(name: String) {
    }
}

protocol nameDelegate {
    func nameThatWasEntered(name: String)
}

class ViewController3: UIViewController {
    @IBOutlet weak var label3: UILabel!
    @IBOutlet weak var textfield3: UITextField!

    var transferedWithSegueLabelText: String = ""
    var namePassBack: nameDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()
        label3.text = transferedWithSegueLabelText
    }

    @IBAction func buttonPressed3(_ sender: Any) {
        namePassBack!.nameThatWasEntered(name: textfield3.text!)
        navigationController?.popToRootViewController(animated: false)
    }
}
Deko Chen
  • 28
  • 4
  • What problem are you having with the code you posted? – rmaddy Sep 29 '18 at 02:42
  • You are calling `namePassBack!.nameThatWasEntered(name: textfield3.text!)`, but `nameThatWasEntered` is empty — it doesn't _do_ anything with the `name` it receives. – matt Sep 29 '18 at 02:45
  • best way is `NotificationCenter` – SPatel Sep 29 '18 at 04:58
  • I think to Unwind segue is the best way for this. Because of NotificationCenter not target point but Unwind segue target point. then its good for use. – Bijender Singh Shekhawat Sep 29 '18 at 05:56
  • Possible duplicate of [How do you pass data between view controllers in Swift?](https://stackoverflow.com/questions/25215476/how-do-you-pass-data-between-view-controllers-in-swift) – dandan78 Sep 29 '18 at 06:00
  • rmaddy, my problem is how do I pass data back to Root View Controller after I clicked the button on ViewController3 – Deko Chen Sep 29 '18 at 17:11
  • yes Matt, thats the problem, I don't know how to set ViewController(the first ViewController) as self of the delegate of ViewController3 :( – Deko Chen Sep 29 '18 at 17:12
  • Nah Unwinding segue doesn't help too much because the view will just keep on stacking, I want the view to go away back to only 1 ViewController and not 1k ViewController stacked :) – Deko Chen Sep 29 '18 at 17:14
  • Spatel Notification Center? Thanks I am going to check on youtube! – Deko Chen Sep 29 '18 at 17:24

2 Answers2

0

I found the way to transfer ViewController3 data to ViewController!

var vc3 = ViewController3()

then making ViewController as the delegate of vc3 upon loading.

override func viewDidLoad() {
    super.viewDidLoad()
    vc3.namePassBack = self
}

Then going back to ViewController3, I add ""vc3."" before using the delegate.

@IBAction func buttonPressed3(_ sender: Any) {
    vc3.namePassBack!.nameThatWasEntered(name: textfield3.text!)
    navigationController?.popToRootViewController(animated: false)
}
Deko Chen
  • 28
  • 4
-1

if you use segue then use 'Unwind segue'

for this write given code in your 'A view controller'

@IBAction func unwindToThisViewController(segue: UIStoryboardSegue) {

}

In B_viewcontroller, On press button call 'Unwind segue'.

Note:- With the help of Unwind segue you can send the value for 3ed vc to 1se vc same as you send value form 1st vc to 2nd vc

for more go to this link

Bijender Singh Shekhawat
  • 3,934
  • 2
  • 30
  • 36
  • I have a question Bijender Singh Shekhawat, does using Unwinding segue makes a copy of the first ViewController and then stacked on top of the 3rd ViewController? I don't really want that because it will cause performance drop. Thank You tho! – Deko Chen Sep 29 '18 at 17:16
  • no, it does not make a copy of first view controller. it uses your view controller from the stack. – Bijender Singh Shekhawat Sep 30 '18 at 16:18