简体   繁体   English

在swift 3中使用Object执行选择器

[英]Perform Selector With Object in swift 3

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)

and I am calling it like 我称之为

viewC.perform(Selector.init("imageSelected:"), with: image, afterDelay: 0.1)

But the app crashes with error that the selector is not defined. 但应用程序崩溃时出现错误,未定义选择器。

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)

It started working well as, I modified the selector being called 它开始运作良好,我修改了被调用的选择器

from

func imageSelected(aImage : UIImage)

to this 对此

func imageSelected(_ aImage : UIImage)

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM