ios swift 3.0 的选择器语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39205613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 10:00:18  来源:igfitidea点击:

Selector syntax for swift 3.0

iosiphoneswiftswift3uibutton

提问by Ram Mani

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'

使用未解析的标识符“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.

谁能告诉我在swift 3中执行此操作的正确方法是什么?

回答by Anbu.Karthik

Add target like,

添加目标,例如,

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

现在应该写成#selector(buttonTapped(sender:))或使用#selector(buttonTapped(_:))

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

then change your function like,

然后改变你的功能,比如

@objc func buttonTapped(_ sender : UIButton){

 ....
 }

回答by Dharmesh Kheni

You can do it this way:

你可以这样做:

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