[英]UIControl.appearance().addTarget stopped working on Swift 4
I have code that stopped working when I switched from Swift 3.2 to Swift 4.0. 从Swift 3.2切换到Swift 4.0时,我的代码停止工作。 It's goal is to add click action to all UIButtons and UISwitches.
目的是向所有UIButton和UISwitch添加点击操作。
private func turnSound(state: Bool) {
UIButton.appearance().isSoundEnabled = state
UISwitch.appearance().isSoundEnabled = state
}
extension UIControl {
var isSoundEnabled: Bool {
get {
return target(forAction: #selector(AudioManager.playStandardClickSound), withSender: nil) != nil ? true : false
}
set {
if newValue {
addTarget(AudioManager.manager, action: #selector(AudioManager.playStandardClickSound), for: .touchUpInside)
} else {
removeTarget(AudioManager.manager, action: #selector(AudioManager.playStandardClickSound), for: .touchUpInside)
}
}
}
}
It worked well before, but now I'm getting an exception: 以前效果不错,但现在我遇到了一个例外:
'NSInvalidArgumentException', reason: '*** Illegal axis type, : for appearance setter, addTarget:action:forControlEvents:. Expected NSInteger or NSUInteger'
And I don't understand how to get rid of this problem. 而且我不知道如何摆脱这个问题。
Will be glad to any help or alternative solution. 乐于提供任何帮助或替代解决方案。
UPDATE: 更新:
You need to add @objc
to your extension too: 您还需要在扩展名中添加
@objc
:
@objc extension UIControl
PS Credits goes to this answer https://stackoverflow.com/a/44391389/2241008 PS点数转到此答案https://stackoverflow.com/a/44391389/2241008
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.