I am facing issue in adding target action to a UIButton created in a custom UIView class.
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
The configure(with viewModel: ReActivateSubscriptionViewPresentable)
method is called after I instantiate the ReActivateSubscriptionView.
The question is a little unclear.
But here's how you correctly subclass UIButton
so that it "knows" it has been clicked:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.