简体   繁体   English

无法在自定义视图 class 中向 class UIButton 添加目标操作

[英]Not able to add target action to a class UIButton inside a custom View class

I am facing issue in adding target action to a UIButton created in a custom UIView class.我在将目标操作添加到在自定义 UIView class 中创建的 UIButton 时遇到问题。

Please look into this code for explanation-请查看此代码以进行解释-

public class ReActivateSubscriptionView: UIView {

    @IBOutlet weak var reActiveSubscriptionLabel: UILabel!
    @IBOutlet weak var activateNowButton: UIButton!
    @IBOutlet weak var collapseSubscribeViewButton: UIButton!


    var viewModel: ReActivateSubscriptionViewPresentable! {
        didSet {
            viewModel.delegate = self
        }
    }

    func configure(with viewModel: ReActivateSubscriptionViewPresentable) {
        self.viewModel = viewModel
        configureSubViews()
    }

    public static var isCollapsed = false

    public class var collapsedActivateNowBtn: UIButton {
        let activateButton = UIButton()

        activateButton.frame = CGRect(x: 16.0, y: 112, width: UIScreen.main.bounds.width - 32, height: 33)
        activateButton.applyStyle(Style.reActivateSubscriptionConfig.activateButton, text: Asset.String.reActivateButtonText, textAlignment: .center, shouldUnderline: true)
        activateButton.addCornerRadius(radius: 6.0)
        return activateButton
    }

    public func configureSubViews() {
        self.backgroundColor = .black
        self.reActiveSubscriptionLabel.applyStyle(Style.reActivateSubscriptionConfig.reActivateSubscribeLabel, text: Asset.String.reActivateText, textAlignment: .center)
        self.activateNowButton.applyStyle(Style.reActivateSubscriptionConfig.activateButton, text: Asset.String.reActivateButtonText, textAlignment: .center, shouldUnderline: true)
        self.addCornerRadius(radius: 6.0)
        self.collapseSubscribeViewButton.setImage(Asset.Image.whiteUpArrow, for: .normal)
        self.collapseSubscribeViewButton.addTarget(self, action: #selector(collapseActivateView), for: .touchUpInside)
        self.activateNowButton.addTarget(self, action: #selector(activateSubscription), for: .touchUpInside)
        ReActivateSubscriptionView.collapsedActivateNowBtn.addTarget(self, action: #selector(activateSubscription), for: .touchUpInside)
    }

    @objc func collapseActivateView() {
        viewModel.handleCollapseTap()
    }

    @objc func activateSubscription() {
        viewModel.activateSubscription()
    }
}

I am trying to add the target action to my collapsedActivateNowBtn , but no action is performed on adding this target in configureSubViews我正在尝试将目标操作添加到我的collapsedActivateNowBtn ,但在configureSubViews添加此目标时未执行任何操作

The configure(with viewModel: ReActivateSubscriptionViewPresentable) method is called after I instantiate the ReActivateSubscriptionView. configure(with viewModel: ReActivateSubscriptionViewPresentable)方法在我实例化 ReActivateSubscriptionView 后被调用。

The question is a little unclear.这个问题有点不清楚。

But here's how you correctly subclass UIButton so that it "knows" it has been clicked:但这里是你如何正确子类UIButton以便它“知道”它已被点击:

import UIKit

class MyButton: UIButton {

    override func didMoveToSuperview() {
        super.didMoveToSuperview()
        addTarget(self, action: #selector(clicked), for: .touchUpInside)
    }

    @objc func clicked() {
        print("I have been clicked")
    }

}

(As a rule, always subclass . Don't try to add behaviors you need "from above" - subclass.) (作为一项规则,总是子类。不要试图添加你需要“从上面”的行为 - 子类。)

Again it's unclear exactly what you're trying to do, OP, but this is how you make a button "know" what is happening.再次,您不清楚您要做什么,OP,但这就是您如何让按钮“知道”正在发生的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM