带有 touchUpInside 的 UIButton 选择器在使用 Swift 4 的嵌套 UIView 中不起作用

UIButton selector with touchUpInside is not working in nested UIViews using Swift 4

当我在嵌套的 UIView 中使用 UIButton 的选择器时,Tap 事件不起作用,但是如果我将 UIButton 及其选择器放在嵌套的 UIView 之外(在根视图中),它会识别 tap 事件并打印 "TAP TAP" 在控制台中。

我正在使用 swift 4.

This is the UIView that contains the login Button and it is a subview of the root view

let blueBottomView: UIView = {
    let bv = UIView()
    bv.backgroundColor = UIColor.MyColors.Blue.Light
    return bv
}()

This is the button and the tap event

let loginButton: UIButton = {
   let  lb = UIButton()
    lb.layer.cornerRadius = 16.0
    lb.layer.borderColor = UIColor.MyColors.Yellow.Light.cgColor
    lb.layer.borderWidth = 2.0
    lb.backgroundColor = UIColor.MyColors.Yellow.Light
    lb.setAttributedTitle(MyHelpers().createAttributeString(text: "Iniciar sesión", color: .white, bold: true), for: .normal)
    lb.addTarget(self, action: #selector(tapLoginButton), for: .touchUpInside)

    return lb
}()


@objc func tapLoginButton(){
    print("TAP TAP")
}

This is the section where the button becomes a child of the nested UIView

func createView(isLogin: Bool = true){
    addSubview(logoImageView)
    addSubview(loginSwitchButton)
    addSubview(registerSwitchButton)
    addSubview(lineSwitching)

    blueBottomView.addSubview(userTextField)
    blueBottomView.addSubview(passwordTextField)
    if (isLogin){
        confirmPasswordTextField.removeFromSuperview()
        createAccountButton.removeFromSuperview()
        blueBottomView.addSubview(loginButton)
        blueBottomView.addSubview(forgotPassword)
        blueBottomView.addSubview(redesSocialesLabel)
        blueBottomView.addSubview(facebookButton)
        blueBottomView.addSubview(googleButton)
    }else{
        loginButton.removeFromSuperview()
        forgotPassword.removeFromSuperview()
        redesSocialesLabel.removeFromSuperview()
        facebookButton.removeFromSuperview()
        googleButton.removeFromSuperview()
        blueBottomView.addSubview(confirmPasswordTextField)
        blueBottomView.addSubview(createAccountButton)
    }


    addSubview(blueBottomView)

    _ =  logoImageView.anchor(centerYAnchor, left: nil, bottom: nil, right: nil, topConstant: -250, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 354, heightConstant: 124)

    logoImageView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true

    _ =  loginSwitchButton.anchor(logoImageView.bottomAnchor, left: leftAnchor, bottom: nil, right: nil, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: frame.width/2, heightConstant: 35).first

    _ =  registerSwitchButton.anchor(logoImageView.bottomAnchor, left: nil, bottom: nil, right: rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: frame.width/2, heightConstant: 35).first

    _ =  lineSwitching.anchor(registerSwitchButton.bottomAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 5)

    blueBottomView.anchorToTop(lineSwitching.bottomAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor)

    _ = userTextField.anchor(lineSwitching.bottomAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, topConstant: 20, leftConstant: 32, bottomConstant: 0, rightConstant: 32, widthConstant: 0, heightConstant: 35)

    _ = passwordTextField.anchor(userTextField.bottomAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, topConstant: 16, leftConstant: 32, bottomConstant: 0, rightConstant: 32, widthConstant: 0, heightConstant: 35)


    if (isLogin){
        _ = loginButton.anchor(passwordTextField.bottomAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, topConstant: 16, leftConstant: 82, bottomConstant: 0, rightConstant: 82, widthConstant: 0, heightConstant: 35)

        _ = forgotPassword.anchor(loginButton.bottomAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, topConstant: 16, leftConstant: 82, bottomConstant: 0, rightConstant: 82, widthConstant: 0, heightConstant: 35)


        redesSocialesLabel.anchorWithConstantsToTop(forgotPassword.bottomAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, topConstant: 25, leftConstant: 16, bottomConstant: 0, rightConstant: 16)//Margins at the left and right hand side

        redesSocialesLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true

        _ = facebookButton.anchor(redesSocialesLabel.bottomAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, topConstant: 16, leftConstant: 52, bottomConstant: 0, rightConstant: 52, widthConstant: 0, heightConstant: 35)

        _ = googleButton.anchor(facebookButton.bottomAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, topConstant: 16, leftConstant: 52, bottomConstant: 0, rightConstant: 52, widthConstant: 0, heightConstant: 35)
    }else{
        _ = confirmPasswordTextField.anchor(passwordTextField.bottomAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, topConstant: 16, leftConstant: 32, bottomConstant: 0, rightConstant: 32, widthConstant: 0, heightConstant: 35)

        _ = createAccountButton.anchor(confirmPasswordTextField.bottomAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, topConstant: 16, leftConstant: 82, bottomConstant: 0, rightConstant: 82, widthConstant: 0, heightConstant: 35)

    }
}

感谢您的帮助!

终于找到解决办法了!我将 let 定义更改为惰性变量,它起作用了!

lazy var loginButton: UIButton = {
 let  lb = UIButton()
 lb.layer.cornerRadius = 16.0
 lb.layer.borderColor = UIColor.MyColors.Yellow.Light.cgColor
 lb.layer.borderWidth = 2.0
 lb.backgroundColor = UIColor.MyColors.Yellow.Light
 lb.setAttributedTitle(MyHelpers().createAttributeString(text:  "Iniciar sesión", color: .white, bold: true), for: .normal)
lb.addTarget(self, action: #selector(tapLoginButton), for: .touchUpInside)

return lb
}()