swift 3选择器与参数
问题描述:
我在Swift 3中搜索了很多选择器方法,但我对它有很多困惑。swift 3选择器与参数
1)Selector
& #selector
有什么区别?
2)如果我用Selector
写的,这个函数概述意味着不可用?
3)如何通过#selector
方法传递参数。
我的代码
let button = UIButton()
button.addTarget(self, action: #selector(getData(_:true)), for: .touchUpInside)
button.addTarget(self, action: Selector(), for: .touchUpInside)
func getData(_ isShowing:Bool){
}
你能不能帮我清除我的困惑?
感谢您的宝贵时间
答
我相信#selector
仅仅是一个语言结构创建Selector
类型的对象。你想使用#selector
作为编译器实际检查方法是否存在任何地方,其中Selector("abc")
你只是运行构造函数,它没有验证。
答
问题的答案:
-
选择是一种类型。 (以表明它是一个函数类型)。而#选择器是调用一个函数。
#selector
- >将返回Selector
类型。#selector
检查功能名称是否存在任何功能 - 第一个答案将阐明此问题
- 您可以像这样通过发件人发送价值。例如:
button.layer.setValue(forKey:"someKey")
+2
至于#3:你不能为任意值做到这一点,只能为那些符合KVO的。关于OP的要求,他不能通过他的'布尔'这种方式。 – Losiowaty
与这一个http://stackoverflow.com/questions/41520683/swift-3-unrecognized-selector-sent-to-instance-uibutton –
关于质询3.比较只需使用'#selector( getData)'不需要指定参数。 –
@LeoDabus,谢谢你的回答,但我必须通过一个值检查条件。 – user1673099