I have a bug where sometimes, for certain users, a button press isn't registered.
I have something similar to the following code. This code works fine for me and most people but some of our users don't see anything happen after tapping the button.
class ViewController: UIViewController {
let nextButton: UIButton = {
let button = UIButton()
button.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
button.setTitleColor(.blue, for: .normal)
button.setTitle("Button", for: .normal)
button.addTarget(self, action: #selector(handleButtonPressed(sender:)), for: .touchDown)
return button
}()
@objc fileprivate func handleButtonPressed(sender: UIButton) {
print("Handle")
}
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(nextButton)
}
}
Changing nextButton
to a lazy var solves the problem for everyone. I assume this is some problem with self not being fully initialized when nextButton
is created and that causes the target/selector registration to get messed up.
My question is what could cause this to fail since I can't reproduce the issue myself?