Suragch
Suragch

Reputation: 511786

How to send message to grandparent View Controller in Swift

I understand how to send data to a child View Controller when calling it with a segue. I think I also understand how to send data to a parent View Controller through the use of protocols and delegates. But how do I send data to a grandparent View Controller?

Options that I've considered:

Upvotes: 0

Views: 1026

Answers (1)

iRiziya
iRiziya

Reputation: 3245

You can use protocols in this case too. I have done this way :

In the current controller(lets say grandchild controller), just intialize your grandparent controller and set delegate(same as you do in prepareForSegues in case of parent controller)

//use this lines when you want call the grandparent controller
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let grandParentVC= storyboard.instantiateViewControllerWithIdentifier("grandPVC") as! UIViewController
grandParentVC.delegate = self
//now call your delegate method here

As you specified, you know protocols (the links you included). Let me know if anything is unclear to you.

Hope this will work for you too!

Upvotes: 2

Related Questions