ios iOS无法识别的选择器发送到Swift中的实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24153058/
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
iOS unrecognized selector sent to instance in Swift
提问by Stephen Fox
I am having problems with trying to get a UIButton to work when the user presses it. I keep getting an error saying: unrecognised selector sent to instance
当用户按下 UIButton 时,我在尝试让 UIButton 工作时遇到问题。我不断收到错误消息:无法识别的选择器已发送到实例
override func viewDidLoad() {
super.viewDidLoad()
button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
button.setTitle("Print", forState: UIControlState.Normal)
button.font = UIFont(name: "Avenir Next", size: 14)
button.backgroundColor = UIColor.lightGrayColor()
self.view.addSubview(button)
}
func buttonClick(Sender: UIButton!)
{
myLabelInfo.text = "Hello"
}
For a Swift method such as func buttonClick(Sender: UIButton)
what is the correct string to pass to addTarget
method for the selector? Is it "buttonClick", "buttonClick:", "buttonClickSender:" or something else?
对于 Swift 方法,例如func buttonClick(Sender: UIButton)
传递给addTarget
选择器方法的正确字符串是什么?是“buttonClick”、“buttonClick:”、“buttonClickSender:”还是别的什么?
回答by Mick MacCallum
You're using an invalid method signature for the action. You're supplying buttonClick
, but the method has an argument, so the signature should be buttonClick:
您正在为该操作使用无效的方法签名。您正在提供buttonClick
,但该方法有一个参数,因此签名应该是buttonClick:
button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
For more information about how to format your selectors, you can refer to the accepted answer in the post linked below. The code used in this post may be Objective-C, but all its lessons can be applied here as well.
有关如何设置选择器格式的更多信息,您可以参考下面链接中的已接受答案。这篇文章中使用的代码可能是 Objective-C,但它的所有教训也可以在这里应用。
Creating a selector from a method name with parameters
And as a side note, this code would also be valid if you used Selector("buttonClicked:")
as the action, but you don't have to because string literals can be implicitly cast to the Selector type.
作为旁注,如果您用作Selector("buttonClicked:")
操作,此代码也将有效,但您不必这样做,因为字符串文字可以隐式转换为 Selector 类型。
Quoting from Using Swift with Cocoa and Objective-C
引用在Cocoa 和 Objective-C 中使用 Swift
An Objective-C selector is a type that refers to the name of an Objective-C method. In Swift, Objective-C selectors are represented by the Selector structure. You can construct a selector with a string literal, such as let mySelector: Selector = "tappedButton:". Because string literals can be automatically converted to selectors, you can pass a string literal to any method that accepts a selector.
Objective-C 选择器是一种引用 Objective-C 方法名称的类型。在 Swift 中,Objective-C 选择器由 Selector 结构表示。您可以使用字符串文字构造选择器,例如 let mySelector: Selector = "tappedButton:"。因为字符串文字可以自动转换为选择器,所以您可以将字符串文字传递给任何接受选择器的方法。
回答by Tomasz Wójcik
Swift < 2.2
斯威夫特 < 2.2
In Swift < 2.2 the selector method cannot be private
(unrecognized selector error).
在 Swift < 2.2 中,选择器方法不能是private
(无法识别的选择器错误)。
Prefered (by Apple) notation is the string "methodWithParam:"
notation.
首选(Apple)表示法是字符串"methodWithParam:"
表示法。
Troubleshooting: if you have troubles with NSTimer
selector, maybe your class should be a subclass of NSObject
.
故障排除:如果您在使用NSTimer
选择器时遇到问题,也许您的类应该是NSObject
.
Swift >= 2.2
斯威夫特 >= 2.2
Use the #selector
notation. Read more here: https://github.com/apple/swift-evolution/blob/master/proposals/0022-objc-selectors.md
使用#selector
符号。在此处阅读更多信息:https: //github.com/apple/swift-evolution/blob/master/proposals/0022-objc-selectors.md
For private methods you can use the @objc
method modifier, like this: @objc private func timerTick(timer: NSTimer)
.
对于私有方法可以使用@objc
方法修饰符,就像这样:@objc private func timerTick(timer: NSTimer)
。
No need for subclassing NSObject
anymore!
不再需要子类化NSObject
了!
回答by MkaysWork
The notation for Swift > 2.2 would be:
Swift > 2.2 的表示法是:
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(YourClass.yourMethod(_:)))
Worked for me so far (Xcode 7.3)
到目前为止为我工作(Xcode 7.3)
回答by Erik
You need to pass it an actual Selector
. Try using this line instead:
你需要传递一个实际的Selector
. 尝试改用这一行:
button.addTarget(self, action: Selector("buttonClick:"), forControlEvents: UIControlEvents.TouchUpInside)
You also need the :
at the end of the selector name because you have 1 argument. This is the same as typical Obj-C selector naming.
您还需要:
在选择器名称的末尾,因为您有 1 个参数。这与典型的 Obj-C 选择器命名相同。
回答by saran
You have missed the colon while specifying selector.so the line should be
您在指定选择器时错过了冒号。所以该行应该是
button.addTarget(self, action: Selector("buttonClick:"), forControlEvents: UIControlEvents.TouchUpInside)
button.addTarget(self, action: Selector("buttonClick:"), forControlEvents: UIControlEvents.TouchUpInside)
回答by micmdk
I had the same problem - the solution ended up being prefixing the method I wanted the button to execute on click with @objc
, to expose it in the Objective-C header file and thereby the Objective-C runtime.
我遇到了同样的问题 - 解决方案最终将我希望按钮在单击时执行的方法加上前缀@objc
,以在 Objective-C 头文件中公开它,从而在 Objective-C 运行时中公开它。
Like:
喜欢:
@objc func buttonClick(Sender: UIButton!)
{
myLabelInfo.text = "Hello"
}