简体   繁体   English

swift 无法将目标添加到 UIButton

[英]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.所以这个问题非常简单,但我检查了 100 次,但它仍然无法正常工作。

Basically, when user taps on tableView cell, it open another VC with different views, depending whether or not user is owner of post.基本上,当用户点击 tableView 单元格时,它会打开另一个具有不同视图的 VC,具体取决于用户是否是帖子的所有者。 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() // 在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() //在 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.我没有在任何地方看到黑色 blackButton 的自动布局约束或 label 设置。

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

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