How to use addTarget method in swift 3

113,015

Solution 1

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)

Solution 2

Try this with Swift 4

buttonSection.addTarget(self, action: #selector(actionWithParam(_:)), for: .touchUpInside)
@objc func actionWithParam(sender: UIButton){
    //...
}

buttonSection.addTarget(self, action: #selector(actionWithoutParam), for: .touchUpInside)
@objc func actionWithoutParam(){
    //...
}

Solution 3

Try this

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

Just add parenthesis with name of method.

Also you can refer link : Value of type 'CustomButton' has no member 'touchDown'

Solution 4

  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) {

    }

Solution 5

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)    
}
Share:
113,015
Ninja13
Author by

Ninja13

Updated on March 27, 2020

Comments

  • Ninja13
    Ninja13 about 4 years

    here is my button object

        let loginRegisterButton:UIButton = {
        let button = UIButton(type: .system)
        button.backgroundColor = UIColor(r: 50 , g: 80, b: 130)
        button.setTitle("Register", for: .normal)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setTitleColor(.white, for: .normal)
        button.addTarget(self, action:#selector(handleRegister), for: .touchUpInside)
        return button
    }()
    

    and here is my function

        func handleRegister(){
    
        FIRAuth.auth()?.createUser(withEmail: email, password: password,completion: { (user, error) in
    
            if error != nil
            { print("Error Occured")}
    
            else
            {print("Successfully Authenticated")}
        })        
    }
    

    I'm getting compile error, if addTarget removed it compiles successfully

  • Iulian Onofrei
    Iulian Onofrei over 7 years
    Actually, don't add any parantheses
  • marlonpya
    marlonpya about 7 years
    ok that is really easy.. but how add a value example.. button.addTarget(self, action:#selector(handleRegister(str: "Hello")), for: .touchUpInside) ?
  • Damien Romito
    Damien Romito about 7 years
    You cannot set parameter in a selector . The action depends on the origin of the target. If you want get the string of the button, get it from the sender in handleRegister(sender: UIButton) let string = sender. currentTitle
  • Kushal Ashok
    Kushal Ashok over 6 years
    One of the workarounds is to set the parameter as the button's accessibility element. But not sure if this is accepted by App Store since I only tried for Enterprise apps.
  • Aron
    Aron over 5 years
    Not a huge issue, but is there still no way to do this without exposing to @objc?
  • Soumen
    Soumen over 5 years
    As Apple Official Documentation (developer.apple.com/documentation/swift/…), you need to use @objc to call your Selector Method, no other "pure swift" way of using the selector.