Selector syntax for swift 3.0

IosIphoneSwiftSwift3Uibutton

Ios Problem Overview


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.

Ios Solutions


Solution 1 - Ios

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 - Ios

You can do it this way:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRam ManiView Question on Stackoverflow
Solution 1 - IosAnbu.KarthikView Answer on Stackoverflow
Solution 2 - IosDharmesh KheniView Answer on Stackoverflow