从xib addTarget加载的按钮不执行操作

时间:2017-12-01 23:16:29

标签: ios swift uibutton

当我想要从加载的xib向按钮添加目标时,我遇到了问题。

我有这个:

var cleanFilters = FilterLabelView()

override func viewWillAppear(_ animated: Bool) {
    navigationItem.title = "EXPLORE WORKOUTS"
    self.navigationController!.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "OpenSans-CondensedBold", size: 16.0)!]
    setFilterLabel()
}

func setFilterLabel() {
    cleanFilters = (Bundle.main.loadNibNamed("FilterLabelView", owner: self, options: nil)?.first as? FilterLabelView)!
    self.view.addSubview(cleanFilters)

    cleanFilters.translatesAutoresizingMaskIntoConstraints = false
    self.view.addConstraint(NSLayoutConstraint(item: cleanFilters, attribute: .top, relatedBy: .equal, toItem: self.topLayoutGuide, attribute: .bottom, multiplier: 1, constant: 30))
    self.view.addConstraint(NSLayoutConstraint(item: cleanFilters, attribute: .trailingMargin, relatedBy: .equal, toItem: self.view, attribute: .trailingMargin, multiplier: 1, constant: 35))

    cleanFilters.deleteButton.addTarget(self, action: #selector(hideFilterLabel), for: .touchUpInside)
    cleanFilters.confirmButton.addTarget(self, action: #selector(setDefaultFilters), for: .touchUpInside)

    cleanFilters.isHidden = defaultsManager.isDefaultFilters()
    if !cleanFilters.isHidden {
        self.workoutsCollection.isUserInteractionEnabled = false
    }
}

func hideFilterLabel() {
    cleanFilters.isHidden = true
    self.workoutsCollection.isUserInteractionEnabled = true
}

func setDefaultFilters() {
    defaultsManager.setDefaultFilters()
    cleanFilters.isHidden = true
    getAllWorkouts()
}

没有调用deleteButton和confirmButton操作,我无法弄清楚原因。

这是我正在加载的FilterLabelView:

import UIKit

class FilterLabelView: UIView {
 @IBOutlet weak var deleteButton: UIButton!
 @IBOutlet weak var confirmButton: UIButton!
 @IBOutlet weak var labelTapRecongnizer: UITapGestureRecognizer!
}

1 个答案:

答案 0 :(得分:0)

你确定你的按钮没有被隐藏吗?请记住,如果一个按钮被隐藏,那么hittest就无法进行触摸......你的按钮是否真的很小或者isUserInteractionEnabled是假的...这是一个很好的方法来了解是否是一个问题视图正在查看Debug view hierarchy