我不知道我做错了什么。我对编程也很陌生,所以我不擅长调试。这是一个测试应用程序,以便我可以看到与应用程序开发的快速联系。到目前为止,我有这个:
class ViewController: UIViewController, UITextViewDelegate {
var textView: UITextView!
init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
override func viewDidLoad() {
super.viewDidLoad()
var widthField = self.view.bounds.size.width - 10
var heightField = self.view.bounds.size.height - 69 - 221
var textFieldString: String! = ""
//Set up text field
self.textView = UITextView(frame: CGRectMake(5, 64, widthField, heightField))
self.textView.backgroundColor = UIColor.redColor()
self.view.addSubview(self.textView)
//Set up the New button
var newButtonString: String! = "New Note"
var heightButton = 568 - heightField - 1000
let newButton = UIButton(frame: CGRect(x: 5, y: 5, width: widthField, height: 50)) as UIButton
UIButton.buttonWithType(UIButtonType.System)
newButton.setTitle(newButtonString,forState: UIControlState.Normal)
newButton.backgroundColor = UIColor.redColor()
newButton.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(newButton)
}
func buttonAction() {
println("tapped button")
}
}
当我按下iOS模拟器中的按钮时,我收到错误“无法识别的选择器发送到实例”。该应用程序打开正常,但只要按下该按钮,它就会崩溃。
答案 0 :(得分:34)
func buttonAction(){...
应该是
func buttonAction(sender:UIButton!)
{
println("tapped button")
}
由于 newButton&#39> <{strong> action: "buttonAction:"
所以。
答案 1 :(得分:12)
如果选择器名称(buttonAction :)中有冒号,则必须将发送者作为buttonAction函数的参数,如果名称中没有冒号,则不会发送发件人。
答案 2 :(得分:9)
我遇到了另外两个可能导致此类错误的原因:
1)当addTarget的第一个参数不从NSObject继承时。例如,这不起作用:
class MyClass {
init(button: UIButton) {
button.addTarget(
self,
action: Selector("onClick:"),
forControlEvents: UIControlEvents.TouchUpInside)
}
func onClick(sender: AnyObject?) {
//Gotcha!
}
}
}
修正只需更改为:
class MyClass: NSObject
...
2)当您没有使用选择器保持实例的显式引用时。例如,如果使用相同的上述代码:
func crashMe() {
var delegate = MyClass(button)
}
Swift似乎垃圾收集“委托”,即使它正在听按钮,因此会发生崩溃。要修复,请更改为以下内容:
func crashMe() {
self.delegate = MyClass(button)
}
在开发我的应用程序时,我也遇到了“私人”和“crashMe”与“crashMe:”问题......因此花时间写这篇文章来总结你将遇到的陷阱。 :)
答案 3 :(得分:9)
通过添加&#34; _&#34;来实现此目的。在参数之前..参考下面的片段
plot(x = user_sig_all$BeginTime, y = factor(user_sig_all$APName), yaxt = "n")
countt <- 1
last_lab <- user_sig_all$APName[1]
for (AP in user_sig_all$APName){
if(countt == 1) {
axis(2, at=countt:countt, labels=AP)
last_lab <- AP
countt <- countt + 1
} else {
if(last_lab != AP){
axis(2, at=countt:countt, labels=AP)
last_lab <- AP
countt <- countt + 1
}
}
}
答案 4 :(得分:6)
语法应为
@IBAction func buttonAction(_ sender: UIButton) {
print('tapped')
}
语法是问题还是
故事板上的按钮有几个已注册的操作。要找到正确的单击按钮,您应该能够看到有多少连接
如果内部修饰有多个连接删除一个。 这是因为如果你有多个连接并且你点击了控件,编译器会对它应该实现哪个连接感到困惑
答案 5 :(得分:3)
我知道现在回答这个问题为时已晚,但是对于那些偶然遇到类似问题的人,我正在回答这个问题。最近,我遇到了一个“无法识别的选择器发送到实例”的问题,这使我花了一整天的时间进行调试。因此,我列出了该问题的可能原因,希望这可以帮助面临此错误的人。
通常,将选择器传递给实例但该选择器不存在时会引发此错误。用简单的话来说,就像有人给快递员一个地址要发送,但是那个地址不存在。
如何调试: 1.首先,检查所有带有@objc标记的方法:使用#selector()语法调用@objc标记的方法时,方法签名应该完全相同。
由于属性中的键,字典期望“ Any” Xcode不会引发错误。因此,font属性原本希望使用UIFont,但改为使用TextProperty枚举大小写。 因此,请检查您是否在代码中执行了无效的转换。
这些是我能想到的情况。如果您有什么可以帮助您的建议,欢迎您。
答案 6 :(得分:1)
使用performSelector()
方法时,您的方法(匹配选择器)应标记为@objc
{
//...
performSelector(Selector("myMethod"), withObject: nil, afterDelay: 0.3)
//...
// or in swift 2.2
performSelector(#selector(ClassName.myMethod), withObject: nil, afterDelay: 0.3)
//
// or in swift 3
perform(#selector(ClassName.myMethod), with: nil, afterDelay: 0.3)
//
}
@objc private func myMethod() { }
答案 7 :(得分:0)
在你的viewDidLoad中,注意我的Selector用冒号调用我的func。
override func viewDidLoad() {
super.viewDidLoad()
// init gesture recognizer swipe right vars
var rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector("gestureFunc:"))
rightSwipe.direction = UISwipeGestureRecognizerDirection.Right
view.addGestureRecognizer(rightSwipe)
}
然后确保选择器即func已准备好接收它的滑动:
// selector from UISwipeGestureRecognizer
func nextWeapon(gesture: UISwipeGestureRecognizer) {
println("swipe right")
}
答案 8 :(得分:0)
在代码中添加@IBAction
函数,然后在不更新情节提要的情况下删除或重命名该函数,也会导致此错误。
在错误消息中查看选择器:
'-[HelloWorld.ViewController yourActionFunction:]: unrecognized selector sent to instance 0x157e0c830'
^^^^^^^^^^^^^^^^^^
您有一个名为yourActionFunction
的函数吗?如果没有,请选择情节提要,然后在控件上编辑连接(例如按钮)。
答案 9 :(得分:0)
对于那些最终陷入困境的人:如果要在静态环境中创建按钮,请确保将正确的目标传递给UIButton.addTarget(_:action:for:)
。传递self
将导致传递类型而不是实例。