[英]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. 我了解在使用segue调用数据时如何将数据发送到子级 View Controller 。 I think I also understand how to send data to a parent View Controller through the use of protocols and delegates.
我想我也了解如何通过使用协议和委托将数据发送到父级 View Controller 。 But how do I send data to a grandparent View Controller?
但是,如何将数据发送到祖父母 View Controller?
Options that I've considered: 我考虑过的选项:
NSNotificationCenter
. 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. NSNotificationCenter
看起来很不像Swift。 You can use protocols
in this case too. 在这种情况下,您也可以使用
protocols
。 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) 在
current
控制器中(让我们说grandchild
控制器),只需初始化您的grandparent
控制器并设置delegate
(与在parent
控制器的prepareForSegues
中所做的相同)
//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). 如您所指定的,您知道
protocols
(包括的链接)。 Let me know if anything is unclear to you. 让我知道您是否不清楚。
Hope this will work for you too! 希望这也对您有用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.