51

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.

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Ram Mani
  • 1,009
  • 1
  • 10
  • 15
  • 2
    Show us your buttonTapped method. IBAction methods can have 0, 1, or 2 parameters depending on how you set them up, and we can't know what form you used. – Duncan C Aug 29 '16 at 11:53

2 Answers2

83

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

 ....
 }
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
22

You can do it this way:

btnAll.addTarget(self, action: #selector(buttonTapped(sender:)), for: .touchUpInside)
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165