ios 在 swift 3 中使用对象执行选择器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41951473/
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
Perform Selector With Object in swift 3
提问by Zaheer Abbas
I am trying to perform selector with object in swift 3.0
我正在尝试在swift 3.0 中使用对象执行选择器
I have a selector which have one parameter
我有一个选择器,它有一个参数
func imageSelected(aImage : UIImage)
func imageSelected(aImage : UIImage)
and I am calling it like
我这样称呼它
viewC.perform(Selector.init("imageSelected:"), with: image, afterDelay: 0.1)
viewC.perform(Selector.init("imageSelected:"), with: image, afterDelay: 0.1)
But the app crashes with error that the selector is not defined.
但是应用程序因未定义选择器的错误而崩溃。
采纳答案by Zaheer Abbas
It started working well as, I modified the selector being called
它开始运行良好,因为我修改了被调用的选择器
from
从
func imageSelected(aImage : UIImage)
to this
对此
func imageSelected(_ aImage : UIImage)
回答by Sweeper
Here's something I always do when I encounter selectors in swift: Ignore the parameters, just use the name.
当我在 swift 中遇到选择器时,我总是这样做:忽略参数,只使用名称。
You used this:
你用过这个:
imageSelected:
What is that :
doing there? Delete it! Just use the name of the method!
那:
在那里做什么?删除它!只需使用方法的名称!
Also, there is this great #selector
syntactic sugar, please use that:
此外,还有这个很棒的#selector
语法糖,请使用:
viewC.perform(#selector(imageSelected), with: image, afterDelay: 0.1)
回答by sonali
This is for swift 4.0
这是为了 swift 4.0
perform(#selector(yourMethodHere), with: nil, afterDelay: 1)
Add @objc flag before your function
在函数前添加 @objc 标志
@objc public func yourMethodHere(){
//your code here
}