容器视图在swift中调用parent func

时间:2015-12-14 17:47:06

标签: swift2.1

在故事板上,父母"场景及其parentVC.swift,有一个带嵌入式segue及其containerVC.swift的containerView 我可以在parentVC viewDidLoad()中调用containerView.myFunc()而没有任何问题。
如何从containerView中的按钮操作调用parentView中定义的自定义func。

self.parentViewController.myCustomFunc()

我得到了

  

UIViewController类型的值?没有成员myCustomFunc

1 个答案:

答案 0 :(得分:1)

You need to cast parentViewController so the compiler knows which functions are available. The definition is simply UIViewController? which, as the error says, doesn't have your function.

Try:

if let vc = self.parentViewController as? parentVC {
    vc.myCustomFunc()
}