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

时间:2019-02-07 17:31:32

标签: ios swift uibarbuttonitem subview

我有一个带有文本字段和按钮的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?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用委托

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() {