3

Here is my problem.

I have ViewController A (let's call it only A).
I push from A to B, which is custom camera controller. When I take picture, I present modally controller C which has two buttons, one for confirm and one for going back.
If back button is tapped, it goes back to B controller.
If confirm button is tapped, C should trigger delegate method to A, and then dismiss C and B, but without animation so dismissing B won't be noticed.

Questions:
1. How to achieve the above described scenario? A needs to be delegate to C, so how to do that? Also should I use unwinding?

Reminder: A -> push -> B -> modal -> C

Slavcho
  • 2,792
  • 3
  • 30
  • 48

2 Answers2

3

I think you can do same functionality with just pass reference of B to C controller

Controller B Code

class BViewController: UIViewController {

    weak var controllerA : ViewController!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

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

    func confirmEventCallFromC() {
        self.navigationController?.popViewController(animated: false)
    }

    @IBAction func btnClick(_ sender: Any) {
         self.performSegue(withIdentifier: "CVC", sender: nil)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "CVC" {
            let vc = segue.destination as! CViewController
            vc.controllerB = self
        }

    }

}

And Controller C Code is like this

class CViewController: UIViewController {

    weak var controllerB : BViewController!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

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

    @IBAction func confirmBtnClick(_ sender: Any) {
        controllerB.confirmEventCallFromC()
        self.dismiss(animated: true, completion: nil)
    }

}

i hope this will help you.

jignesh Vadadoriya
  • 3,244
  • 3
  • 18
  • 29
2

You can make use of UnwindSegue,

You can declare a method in VC A as

@IBAction func unWindToVCA(segue : UIStoryboardSegue) {

}

In VC C storyboard , you can control drag from viewController to exit and you will prompted with a popover which will have a list of all the unwind segues you have declared before. Select the unWindToVCA.

Now select the segue and assign a reusable identifier to it as shown in pic below :)

enter image description here

Finally whenever you want to go back to VC1, call

self.performSegue(withIdentifier: "backToVC1", sender: nil)

Now in VCA you can get the data from VC C using,

@IBAction func unWindToVCA(segue : UIStoryboardSegue) {
       let c = segue.source
       //access c's data and update A
}

I have followed your hierarchy of VC's as well VC A -> push VC B -> modal -> VC C

Community
  • 1
  • 1
Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78