How to use addTarget method in swift 3

后端 未结 10 1276
清酒与你
清酒与你 2020-11-29 02:20

here is my button object

    let loginRegisterButton:UIButton = {
    let button = UIButton(type: .system)
    button.backgroundColor = UIColor         


        
相关标签:
10条回答
  • 2020-11-29 02:56

    Try with swift 3

    cell.TaxToolTips.tag = indexPath.row
            cell.TaxToolTips.addTarget(self, action: #selector(InheritanceTaxViewController.displayToolTipDetails(_:)), for:.touchUpInside)
    
    
     @objc func displayToolTipDetails(_ sender : UIButton) {
            print(sender.tag)
            let tooltipString = TaxToolTipsArray[sender.tag]
            self.displayMyAlertMessage(userMessage: tooltipString, status: 202)    
    }
    
    0 讨论(0)
  • 2020-11-29 02:56
      let button: UIButton = UIButton()
        button.setImage(UIImage(named:"imagename"), for: .normal)
        button.addTarget(self, action:#selector(YourClassName.backAction(_sender:)), for: .touchUpInside)
    
        button.frame = CGRect.init(x: 5, y: 100, width: 45, height: 45)
        view.addSubview(button)
    
        @objc public func backAction(_sender: UIButton) {
    
        }
    
    0 讨论(0)
  • 2020-11-29 02:59

    Yes, don't add "()" if there is no param

    button.addTarget(self, action:#selector(handleRegister), for: .touchUpInside). 
    

    and if you want to get the sender

    button.addTarget(self, action:#selector(handleRegister(_:)), for: .touchUpInside). 
    
    func handleRegister(sender: UIButton){
       //...
    }
    

    Edit:

    button.addTarget(self, action:#selector(handleRegister(_:)), for: .touchUpInside)
    

    no longer works, you need to replace _ in the selector with a variable name you used in the function header, in this case it would be sender, so the working code becomes:

    button.addTarget(self, action:#selector(handleRegister(sender:)), for: .touchUpInside)
    
    0 讨论(0)
  • 2020-11-29 02:59

    Try this with Swift 3

    button.addTarget(self, action:#selector(ClassName.handleRegister(sender:)), for: .touchUpInside)
    

    Good luck!

    0 讨论(0)
提交回复
热议问题