Selector syntax for swift 3.0

57,956

Solution 1

Add target like,

should now be written as #selector(buttonTapped(sender:)) or use #selector(buttonTapped(_:))

btnAll.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)

then change your function like,

@objc func buttonTapped(_ sender : UIButton){

 ....
 }

Solution 2

You can do it this way:

btnAll.addTarget(self, action: #selector(buttonTapped(sender:)), for: .touchUpInside)
Share:
57,956
Ram Mani
Author by

Ram Mani

Updated on October 15, 2020

Comments

  • Ram Mani
    Ram Mani over 3 years

    I am trying to add target into button this way:

    btnAll.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
    

    But it is giving me an error:

    Use of unresolved identifier 'buttonTapped'

    But I declared function like:

    func buttonTapped(sender: UIButton) {
    
        print("All Tapped")
    }
    

    Can anybody tell me what is the correct way to do this in swift 3.

  • Dharmesh Kheni
    Dharmesh Kheni over 7 years
    This is what I have already answered.. :)
  • Nike Kov
    Nike Kov over 7 years
    It changes function to @objc
  • Nike Kov
    Nike Kov over 7 years
    Sorry, i mean, in protocol extensions it needed to change the function to @objc