如何在我的子视图中获取一个按钮以在我的视图控制器中启用 rightbarbuttonitem

How can I get a button in my subview to enable the rightbarbuttonitem in my view controller

我有一个带有文本字段和按钮的 UIView,这个 UIView 是我的视图控制器的子视图。如何让我的子视图中的按钮在按下时启用视图控制器的导航 rightbarbuttonitem?

class CreateActivityView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    lazy var addButton: UIButton = {
        let button = UIButton(type: UIButton.ButtonType.system)
        button.setImage(UIImage(named: "add_dark.png"), for: .normal)
        button.frame = CGRect(x: 0, y: 0, width: 24.0, height: 24.0)
        button.addTarget(self, action: #selector(addButtonTapped), for: .touchUpInside)
        button.isEnabled = false
        button.translatesAutoresizingMaskIntoConstraints = false

        return button
    }()

    lazy var activityNameTextField: DataEntryTextField = {
        let textField = DataEntryTextField()
        textField.frame = CGRect(x: 0, y: 0, width: 250.0, height: 30.0)
        textField.placeholder = "Add an activity"
        textField.font = UIFont(name: "Avenir", size: 17)
        textField.keyboardType = UIKeyboardType.default
        textField.returnKeyType = UIReturnKeyType.done
        textField.translatesAutoresizingMaskIntoConstraints = false

        return textField
    }()

    lazy var activityNameLabel: UILabel = {
        let label = UILabel()
        label.frame = CGRect(x: 0, y: 0, width: 0, height: 30.0)
        label.font = UIFont(name: "Avenir", size: 17)
        label.translatesAutoresizingMaskIntoConstraints = false
        label.isHidden = true

        return label
    }()

    func setupView() {
        backgroundColor = UIColor.lightGray
        addSubview(addButton)
        addButton.rightAnchor.constraint(equalTo: self.safeAreaLayoutGuide.rightAnchor, constant: -16.0).isActive = true
        addButton.centerYAnchor.constraint(equalTo: self.safeAreaLayoutGuide.centerYAnchor).isActive = true
        addButton.heightAnchor.constraint(equalToConstant: 24.0).isActive = true
        addButton.widthAnchor.constraint(equalToConstant: 24.0).isActive = true

        addSubview(activityNameTextField)
        activityNameTextField.leftAnchor.constraint(equalTo: self.safeAreaLayoutGuide.leftAnchor, constant: 16.0).isActive = true
        activityNameTextField.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: 10.0).isActive = true
        activityNameTextField.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor, constant: -10.0).isActive = true
        activityNameTextField.rightAnchor.constraint(equalTo: addButton.safeAreaLayoutGuide.leftAnchor, constant: -20.0).isActive = true

        addSubview(activityNameLabel)
        activityNameLabel.leftAnchor.constraint(equalTo: self.safeAreaLayoutGuide.leftAnchor, constant: 16.0).isActive = true
        activityNameLabel.rightAnchor.constraint(equalTo: self.safeAreaLayoutGuide.rightAnchor, constant: -16.0).isActive = true
        activityNameLabel.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: 10.0).isActive = true
        activityNameLabel.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor, constant: -10.0).isActive = true

    }

    @objc func addButtonTapped() {
        guard let validatedText = activityNameTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines), !validatedText.isEmpty else {
            return
        }

        displayActivityNameLabel(with: validatedText)
    }

    func displayActivityNameLabel(with text: String) {
        activityNameLabel.text = text
        activityNameLabel.isHidden = false
        activityNameTextField.isHidden = true

        print(activityNameLabel.text)
    }

}


class QuickLogViewController: UIViewController {

    let screenSize = UIScreen.main.bounds.size
    var createActivityView: CreateActivityView!

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "Quick Log"
        setupView()

    }

    func setupView() {
        initializeCreateActivityView()
        self.view.addSubview(createActivityView)
        createActivityView.leftAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leftAnchor).isActive = true
        createActivityView.rightAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.rightAnchor).isActive = true
        createActivityView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
        createActivityView.heightAnchor.constraint(equalToConstant: 50.0).isActive = true

    }

    func initializeCreateActivityView() {
        createActivityView = CreateActivityView()
        createActivityView.frame = CGRect(x: 0, y: 0, width: screenSize.width, height: 50.0)
        createActivityView.translatesAutoresizingMaskIntoConstraints = false
        createActivityView.activityNameTextField.delegate = self
    }

    func enableCancelButton() {
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelButtonPressed))
    }
}

我将我的 CreateActivityView 添加到我的视图控制器。按钮目标由我的 createActvitiyView 处理。那么如何从子视图访问rightbarbuttonitem呢?

你可以尝试使用delegate

class CreateActivityView: UIView {
  weak var delegate:VCName?

 createActivityView = CreateActivityView()
 createActivityView.delegate = self

之后在任何动作中做

 delegate?.methodInsideTheVC()

或者在vc

里面写按钮动作
createActivityView = CreateActivityView()
createActivityView.button.addTarget(self, action: #selector(addButtonTapped), for: .touchUpInside)

并放置

@objc func addButtonTapped() {