0

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:

  • Send the data to the parent and have the parent send it to the grandparent. But this seems like a lot of extra work when then parent doesn't need to know.
  • Use 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.
Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • One way is that if you have NavigationController stack then you can using that you can access grandparent and send the data. – Leena Sep 19 '15 at 06:51

1 Answers1

2

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!

iRiziya
  • 3,235
  • 1
  • 22
  • 36
  • 1
    I wouldn't say that I know protocols very well, just a bit of theoretical knowledge. I went a different direction in my app, but until I hear differently I will mark this answer as solving the problem that I presented. – Suragch Sep 22 '15 at 09:07
  • I meant : you know atleast basics of protocols(the links you mentioned) so that you may easily go with my answer :) – iRiziya Sep 22 '15 at 09:10
  • @iRiziya shouldn't the `grandChildVC` be the one which has the `delegate` property? – MrGreen Jan 22 '18 at 20:28