简体   繁体   English

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

[英]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.

相关问题 .xib自定义tableviewcell中的Swift 3按钮未执行操作 - Swift 3 - button in .xib custom tableviewcell is not performing action 从UIView(.xib)按钮加载UIViewController - Load UIViewController from UIView(.xib) Button Action 很多按钮添加相同的addTarget(_:action:for :)方法 - a lot of button add same addTarget(_:action:for:) method 在集合视图的笔尖内将addTarget更改为按钮动作 - addTarget to button action within nib of collection view 维护从xib加载的自定义单元中的按钮状态 - Maintain the button state inside a custom cell loaded from xib 单元上的按钮未执行操作 - Button On Cell Not Performing Action 来自不同XIB的行动 - Action from different XIB 从xib文件中的操作按钮推送另一个Viewcontroller - Push another Viewcontroller from action button in xib file 在.xib文件中创建的UIButton是IBAction还是[UIButton addTarget:action:forControlEvents:]? 哪种方法更好? - IBAction or [UIButton addTarget:action: forControlEvents:] for UIButton created in .xib file? Which is the better approach? Swift 5无法向自定义按钮添加操作(AVPlayerViewController中的addTarget问题) - Swift 5 unable to add action to custom button(addTarget issues in AVPlayerViewController)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM