A
A
alex_6432020-07-18 15:49:41
Swift
alex_643, 2020-07-18 15:49:41

Why doesn't data transfer between VCs work in the opposite direction when clicking on the Bar button item?

Good day! There are two VCs from the second you need to transfer data, the transition to the second is done with the button on the navigation bar. But if the button is on the bar, then the data transfer stops working. How to fix it?
1 VC

import UIKit

class FitstVC: UIViewController {
    @IBOutlet var textLabel: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    
    @IBAction func saveData(_ sender: UIStoryboardSegue) {
        if let sourceViewController = (sender as AnyObject).source as? SecondVC {
            textLabel.text = sourceViewController.text
        }
    }
}

2 VC
import UIKit

class SecondVC: UIViewController {
    var text = ""
    @IBOutlet var textLabel: UILabel!
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        text = "code"
    }
    override func viewDidLoad() {
        super.viewDidLoad()

    }
}

5f12efe155dca772979096.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Vorobei, 2020-07-18
@alex_643

What triggers your action? If the button is back - the method is not called .
In the second controller, there is no data transfer to the first controller at all. You need to use delegates
for your task .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question