1

我在从另一个访问我UIButton的内部时遇到问题。ContainerViewViewController

如果用户点击buttonaSubView应该出现在 my ViewController. button我尝试在我的文件中拖动ViewController以创建一个,@IBAaction func tapped()但这不起作用。

它仅适用于ContainerView.swift文件内部。但我不能Subview在那里创建我的...

我希望你能得到我的问题,我很感激每一个帮助!

在此处输入图像描述

import UIKit

class ContainerViewController: UIViewController {




    override func viewDidLoad() {
        super.viewDidLoad()


        // Do any additional setup after loading the view.
    }


    @IBAction func addButtonTapped(_ sender: Any) {
        print("add Button")         
}
4

2 回答 2

0

您可以使用 NotificationCenter 和 Delegates 实现此目的。这是使用 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.
}
于 2019-11-21T18:27:14.783 回答
0

您可以使用它的属性到达您的ViewController内部。所以,在你的课堂上:ContainerViewparentContainerView

if let parentVC = self.parent as? ViewController {
     parentVC.showSubView() // your method to show the sub view.
}
于 2019-11-21T18:30:46.090 回答