【问题标题】:How to send message to grandparent View Controller in Swift如何在 Swift 中向祖父视图控制器发送消息
【发布时间】:2015-09-19 06:19:20
【问题描述】:

我理解 how to send data to a child View Controller 在使用 segue 调用它时。我想我也通过使用协议和委托来理解how to send data to a parent View Controller。但是如何将数据发送到祖父母视图控制器?

我考虑过的选项:

  • 将数据发送给父级并让父级将其发送给祖父级。但是当父母不需要知道时,这似乎是很多额外的工作。
  • 使用NSNotificationCenter。但是 (1) 只有祖父母需要知道,而不是整个世界,并且 (2) 从我发现的少量 Swift 文档中,NSNotificationCenter 似乎非常不像 Swift。

【问题讨论】:

  • 一种方法是,如果你有 NavigationController 堆栈,那么你可以使用它来访问祖父母并发送数据。

标签: ios swift uiviewcontroller delegates


【解决方案1】:

在这种情况下,您也可以使用protocols。我是这样做的:

current 控制器中(比如grandchild 控制器),只需初始化grandparent 控制器并设置delegate(与prepareForSegues 中的parent 控制器相同)

//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(您包含的链接)。如果您有任何不清楚的地方,请告诉我。

希望这对你也有用!

【讨论】:

  • 我不会说我非常了解协议,只是有点理论知识。我在我的应用程序中采用了不同的方向,但在我听到不同的声音之前,我会将这个答案标记为解决了我提出的问题。
  • 我的意思是:您至少了解协议的基础知识(您提到的链接),这样您就可以轻松地接受我的回答 :)
  • @iRiziya 不应该是 grandChildVC 是具有 delegate 属性的那个吗?
猜你喜欢
  • 2016-05-28
  • 1970-01-01
  • 1970-01-01
  • 2018-08-06
  • 1970-01-01
  • 2015-08-06
  • 1970-01-01
  • 1970-01-01
  • 2011-12-29
相关资源
最近更新 更多