0

I have a view controllerA with segmented control, and I have added two view controller's(B & C) views in controllerA on different segment selection.I have one button each on controllerB and controllerC.On button click of each controllerB & controllerC, I am going to controllerD.

How do I know from which controller I am coming from?

I have tried code below but I think due to views of controller's (B & C) added to controllerA, it is giving me nil.

guard let parent = self.parent else {return}

How to get parent in this scenario?

Zahid Shaikh
  • 37
  • 10
  • Did you try self.presentingViewController ? – ekscrypto Jul 19 '18 at 07:33
  • If you are modally presenting D then u can say `self.presentingViewController` to get the reference to parent or if its being pushed you can say `self.parent` in D to get parent viewController – Sandeep Bhandari Jul 19 '18 at 07:34
  • On a slightly different note, you can always declare a weak reference called parent in ViewController D declare a protocol and Make your ViewController B and C to confirm to it and pass your ViewController B or C's instance to D while presenting or pushing. Use protocol methods to trigger the methods in B and C – Sandeep Bhandari Jul 19 '18 at 07:37
  • Actually if you added a `viewController` as child then you will be able to get the way you tried. Otherwise it is difficult to tell without knowing the way you are navigating. You might have to share more code on how you are adding/presenting `B`,`C` and `D` `ViewControllers` – Kamran Jul 19 '18 at 07:43
  • self.presentingViewController also giving me nil. – Zahid Shaikh Jul 19 '18 at 07:52
  • Are you using segues? – Asim Jul 19 '18 at 09:50

1 Answers1

2

A slightly different solution I used once:

  • Declare an enum with sender A, B, C etc.

    enum Sender {
        case A
        case B
        case C
    }
    
  • Put a variable in D called sender.

     var sender : Sender!
    
  • On initializing VC set it's respective sender. If you are using segue use prepare for segue to set value.

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
        if segue.identifier == "D" {
            let vc = segue.destination as! D
            vc.sender = B or C //As required
        }
    }
    

Then you can use the value of sender to do what ever you want based on the sender. The good thing here is if you keep navigating you can always propagate the sender value to next ViewControllers.

Asim
  • 361
  • 1
  • 2
  • 12
  • thank for the solution but the problem is I am not using segue as I am creating the xib of controller's(B &C),and then adding these controllers views to the controllerA and then navigating to controllerD – Zahid Shaikh Jul 19 '18 at 10:35
  • How do you navigate to controller D? And do you present it or push it on A? – Asim Jul 19 '18 at 10:47
  • I am navigating to controllerD through the button click inside the controller's(B&C) views,which are added as subview to viewControllerA.I am using self.present to navigate to controllerD – Zahid Shaikh Jul 19 '18 at 11:55
  • You can do: let vc = MyViewController(nibName: "D", bundle: nil); vc.sender = B or C; self.present(vc, animated: true, completion: nil); – Asim Jul 19 '18 at 12:00