简体   繁体   中英

swift cannot add target to UIButton

so this question is super simple, but i checked that like a 100 times but it still won't work.

Basically, when user taps on tableView cell, it open another VC with different views, depending whether or not user is owner of post. First condition, works just fine, adding target to button, while when second is being executed, nothing happens

lazy var buttonsView = DetailButtonsView() // those are almost the same
lazy var addvertView = AdvertiseView()

// inside of buttonsView()

lazy var skipButton: UIButton = {
    let button = UIButton()
    button.setTitle("Пожаловаться", for: .normal)
    button.setTitleColor(.mainColor, for: .normal)
    button.titleLabel?.font = .getPoppinsMediumFont(on: 15)

    return button
}()
override init(frame: CGRect) {
    super.init(frame: .zero)
    setupView() //set up constraints
}

//inside of AdvertiseView()

    lazy var blackButton:UIButton = {
      var button = UIButton()
    button.layer.cornerRadius = 8
      button.backgroundColor = .black
      return button
  }()
override init(frame: CGRect) {
    super.init(frame: .zero)
    setupView() //set up constraints 
}

//

 func setUpBottom() -> Void {

    if  dataInfo!.user_id! == UserSettings.userModel.id
    {
        self.backView.addSubview(addvertView) //also works

          addvertView.snp.makeConstraints //works
          {
              (make) in
              make.left.equalToSuperview()
              make.top.equalTo(userView.snp.bottom).offset(24)
              make.height.equalTo(450)
              make.bottom.lessThanOrEqualTo(-34)
          }


          addvertView.blackButton.addTarget(self, action: #selector(blackButtonMethod), for: .touchUpInside) // does not add target 


    }
    else  {
        backView.addSubview(buttonsView) //works

        buttonsView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(userView.snp.bottom).offset(24)
            make.bottom.lessThanOrEqualTo(-34)
        }
        buttonsView.skipButton.addTarget(self, action: #selector(toComplain), for: .touchUpInside) //works



    }

 @objc func toComplain(){ //works
    let vc = ComplaintTypeViewController()
    vc.advertID = dataInfo!.id!
    navigationController?.pushViewController(vc, animated: true)
}
@objc func blackButtonMethod(){ //does not work
    print("hello")
    parameters["action"] = "hot"
    parameters["advert_id"] = String(describing: dataInfo!.id)
    updateAdvert(parameters: parameters)
}

The target is probably added, but you cannot interact with it. That usually happens when constraints/frame is not set right. I see that the skip button uses:

button.setTitle("Пожаловаться", for: .normal)

which will infer autolayout width/height.

I don't see black blackButton's autolayout constraints or label set anywhere.

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.

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