[英]Button loaded from xib addTarget not performing action
I'm having a problem when I want to add a target to a button from a loaded xib. 我要从已加载的xib向按钮添加目标时遇到问题。
I have this: 我有这个:
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()
}
The deleteButton and confirmButton actions are not being called and I can't figure out why. deleteButton和confirmButton操作没有被调用,我不知道为什么。
Here's the FilterLabelView I'm loading: 这是我正在加载的FilterLabelView:
import UIKit
class FilterLabelView: UIView {
@IBOutlet weak var deleteButton: UIButton!
@IBOutlet weak var confirmButton: UIButton!
@IBOutlet weak var labelTapRecongnizer: UITapGestureRecognizer!
}
Are you sure that your buttons are not hidden? 您确定按钮没有隐藏吗? remember that if a button is hidden the hittest can't perform touches... is it also posible your buttons are really little or have
isUserInteractionEnabled
as false... A good way to be aware whether is a problem with the view is looking out the Debug view hierarchy
请记住,如果隐藏了按钮,则击键测试将无法执行触摸操作...是否还可能是按钮确实很少或具有
isUserInteractionEnabled
为false ...一种很好的方式来了解视图是否存在问题? Debug view hierarchy
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.