Selector syntax for swift 3.0

Ram Mani picture Ram Mani · Aug 29, 2016 · Viewed 57.5k times · Source

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.

Answer

Anbu.Karthik picture Anbu.Karthik · Aug 29, 2016

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

 ....
 }