我在从另一个UIButton
访问ContainerView
内的ViewController
时遇到问题。
如果用户点击button
,则SubView
应该出现在我的ViewController
内部。我尝试将button
文件中的ViewController
拖动到创建@IBAaction func tapped()
,但这无法正常工作。
仅在ContainerView.swift
文件内部起作用。但是我无法在此处创建我的Subview
...
希望您能解决我的问题,感谢您的帮助!
您可以使用NotificationCenter来实现。
在您的ContainerViewController中,您可以在单击按钮时发布通知
@IBAction func yourBtnTap(sender : UIButton) {
NotificationCenter.default.post(name: Notification.Name("myBtnTapped"), object: nil)
}
在CurrentViewController中,您必须先注册才能接收此通知。因此,在您的viewDidLoad中,执行以下操作:
NotificationCenter.default.addObserver(self, selector: #selector(self.myBtnTappedAction(notification:)), name: Notification.Name("myBtnTapped"), object: nil)
现在在CurrentViewContoller中创建myBtnTappedAction函数,只要轻按按钮即可调用该函数。
@objc func myBtnTappedAction(notification : Notification) {
// now you can perform all your actions here. this function will be called everytime the button in the container view is tapped.
}
您可以使用ViewController
属性将其ContainerView
移到parent
内部。因此,在您的ContainerView
类中:
self.parent?.showSubView() // your method to show the sub view.