Thomas Marques
Thomas Marques

Reputation: 1051

Passing back data to first viewController

I have a basic understanding of how to handle swift protocols to pass data between two viewcontrollers. My situation is this: I wanted to pass data back to my first viewcontroller.

For example:

First ViewController

class ViewControllerA: UIViewController, ViewControllerCResult {

    func set(data: String) {

    }

}

Second ViewController

class ViewControllerB: UIViewController {

}

Third ViewController

protocol ViewControllerCResult {
   set(data: String)
}

class ViewControllerC: UIViewController {
    var delegate: ViewControllerCResult?
}

ViewControllerA -> ViewControllerB -> ViewControllerC

I would like to pass data from ViewControllerC to ViewControllerA. Can anyone help me with this?

Upvotes: 0

Views: 101

Answers (1)

Muhammad Shauket
Muhammad Shauket

Reputation: 2778

You can pass Delegates from Controller A -> B -> C , and when popViewcontroller will be called you can check the self.delegate in ViewController C, if it exist just call function like this self.delegate?.set("data from c"). Check

popControllerPressed Function in ViewController3

First View controller

class ViewController: UIViewController, ViewControllerCResult {

var delegate: ViewControllerCResult?

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func pushControllerPressed(_ sender: Any) {
    if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController2") as? ViewController2 {
        if let navigator = navigationController {
            viewController.delegate = self
            navigator.pushViewController(viewController, animated: true)
        }
    }
}

func set(data: String) {

}

}

Second ViewController

class ViewController2: UIViewController, ViewControllerCResult {

var delegate: ViewControllerCResult?

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func pushControllerPressed(_ sender: Any) {
    if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController3") as? ViewController3 {
        if let navigator = navigationController {
            viewController.delegate = self.delegate
            navigator.pushViewController(viewController, animated: true)
        }
    }
}

func set(data: String) {
    print(data)
}

}

Third ViewController

protocol ViewControllerCResult {
   func set(data: String)
}

class ViewController3: UIViewController, ViewControllerCResult {

var delegate: ViewControllerCResult?

func set(data: String) {
    print(data)
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func popControllerPressed(_ sender: Any) {
    for controller in self.navigationController!.viewControllers as Array {
        if controller.isKind(of: ViewController.self) {
            delegate?.set(data: "data from C")
            self.navigationController!.popToViewController(controller, animated: true)
            break
        }
    }

}

}

Here is Code Link: Pass data forth and back

Upvotes: 1

Related Questions