Ios Swift-访问另一个ViewController中ContainerView内的UIButton
从另一个Ios Swift-访问另一个ViewController中ContainerView内的UIButton,ios,swift,uiviewcontroller,uicontainerview,Ios,Swift,Uiviewcontroller,Uicontainerview,从另一个ViewController访问ContainerView中的ui按钮时遇到问题 如果用户点击按钮,则子视图应显示在我的视图控制器中。我尝试在我的ViewController文件中拖动按钮来创建@IBAaction func tapped(),但不起作用 它只在ContainerView.swift文件中工作。但是我不能在那里创建我的子视图 我希望你能解决我的问题,我感谢你的帮助 您可以使用NotificationCenter和委托来实现这一点。下面是您如何使用Notification
ViewController
访问ContainerView
中的ui按钮时遇到问题
如果用户点击按钮
,则子视图
应显示在我的视图控制器
中。我尝试在我的ViewController
文件中拖动按钮来创建@IBAaction func tapped()
,但不起作用
它只在ContainerView.swift
文件中工作。但是我不能在那里创建我的子视图
我希望你能解决我的问题,我感谢你的帮助
您可以使用NotificationCenter和委托来实现这一点。下面是您如何使用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)
现在,在CurrentViewConcoller中创建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.
}
您可以使用NotificationCenter和委托来实现这一点。下面是您如何使用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)
现在,在CurrentViewConcoller中创建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
类中:
if let parentVC = self.parent as? ViewController {
parentVC.showSubView() // your method to show the sub view.
}
您可以使用其父属性访问容器视图中的ViewController
。因此,在您的ContainerView
类中:
if let parentVC = self.parent as? ViewController {
parentVC.showSubView() // your method to show the sub view.
}
你能添加屏幕截图吗?那会有帮助的!当然可以。更新了答案。你能添加屏幕截图吗?那会有帮助的!当然可以。更新了答案。谢谢你的答案:)但是@Muhamed Gül answers看起来更简单,因为我无法找到另一个答案,我试过了,效果很好:)谢谢!很高兴为您提供帮助:)谢谢您的回答:)但是@Muhamed Gül answers对我来说看起来更简单,因为我无法找到另一个答案,我试过了,效果很好:)谢谢!很乐意帮忙:)这对我没用。我无法从我的ContainerVIew中的ViewController
访问任何func
,如果我已经更新了答案,请重新检查。这对你有用吗??因为我得到了6个错误:D另一个答案实际上对我有效,即使这不是很好smooth@DieGlueckswurst是的,它工作得很好。你犯了什么错误?我希望您没有尝试使用名为showSubView()的方法,它只是一个示例。我还假设您的父视图控制器的类是ViewController
,因为在您的问题中看起来是这样的。如果不是,将更改为?ViewController
与相同?YourParentClassName
或其他任何名称。它对我不起作用。我无法从我的ContainerVIew中的ViewController
访问任何func
,如果我已经更新了答案,请重新检查。这对你有用吗??因为我得到了6个错误:D另一个答案实际上对我有效,即使这不是很好smooth@DieGlueckswurst是的,它工作得很好。你犯了什么错误?我希望您没有尝试使用名为showSubView()的方法,它只是一个示例。我还假设您的父视图控制器的类是ViewController
,因为在您的问题中看起来是这样的。如果不是,将更改为?ViewController
与相同?YourParentClassName
或它的任何名称。