[英]How to send message to grandparent View Controller in Swift
我了解在使用segue调用数据时如何将数据发送到子级 View Controller 。 我想我也了解如何通过使用协议和委托将数据发送到父级 View Controller 。 但是,如何将数据发送到祖父母 View Controller?
我考虑过的选项:
NSNotificationCenter
。 但是(1)只需要了解祖父母,而不是整个世界,以及(2)从我发现的Swift文档很少, NSNotificationCenter
看起来很不像Swift。 在这种情况下,您也可以使用protocols
。 我这样做:
在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
如您所指定的,您知道protocols
(包括的链接)。 让我知道您是否不清楚。
希望这也对您有用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.