Reputation: 511786
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:
NSNotificationCenter
. But (1) it is only the grandparent that needs to know, not the whole world, and (2) from the scant Swift documentation that I've found, NSNotificationCenter
seems very unSwiftlike.Upvotes: 0
Views: 1026
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