在swift中"无法识别的选择器发送到实例"

eli*_*o25 27 swift

我不知道我做错了什么.我对编程也很陌生,所以我不擅长调试.这是一个测试应用程序,以便我可以看到与应用程序开发的快速联系.到目前为止,我有这个:

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")
    }
}
Run Code Online (Sandbox Code Playgroud)

当我按下iOS模拟器中的按钮时,我收到错误"无法识别的选择器发送到实例".该应用程序打开正常,但只要按下该按钮,它就会崩溃.

iPa*_*tel 35

func buttonAction(){...
Run Code Online (Sandbox Code Playgroud)

应该

func buttonAction(sender:UIButton!)
{
    println("tapped button")
}
Run Code Online (Sandbox Code Playgroud)

因为newButton action: "buttonAction:"所以.

  • 对于所有未来的Google员工,请注意,使用`private func`时它不起作用.@matsmats也是正确的,当他说如果你包含一个":",例如`"buttonAction:"`,那么你必须将发送者作为参数传递给调用.否则,包含该参数将使方法调用失败. (10认同)

mat*_*ats 12

如果选择器名称(buttonAction :)中有冒号,则必须将发送方作为buttonAction函数的参数,如果名称中没有任何冒号,则不会发送发送方.


alp*_*com 9

我遇到了另外两个可能导致此类错误的原因:

1)当addTarget的第一个参数不从NSObject继承时.例如,这不起作用:

class MyClass {
    init(button: UIButton) {
        button.addTarget(
            self,
            action: Selector("onClick:"),
            forControlEvents: UIControlEvents.TouchUpInside)
    }

    func onClick(sender: AnyObject?) {
        //Gotcha!
    }
}
Run Code Online (Sandbox Code Playgroud)

}

要修改只是更改为:

class MyClass: NSObject
...
Run Code Online (Sandbox Code Playgroud)

2)当您没有使用选择器保持实例的显式引用时.例如,如果使用相同的上述代码:

func crashMe() {
    var delegate = MyClass(button)
}
Run Code Online (Sandbox Code Playgroud)

Swift似乎垃圾收集"委托",即使它正在听按钮,因此会发生崩溃.要修复,请更改为以下内容:

func crashMe() {
    self.delegate = MyClass(button)
}
Run Code Online (Sandbox Code Playgroud)

在开发我的应用程序时,我也遇到了"私有"和"crashMe"与"crashMe:"问题......因此花时间写这篇文章来总结你将遇到的陷阱.:)


小智 9

通过在参数前添加"_"来实现此功能..请参阅下面的代码段

func buttonAction(sender:UIButton)  // this was throwing the error "unrecognized selector sent to instance"
{
    println("tapped button")
}

func buttonAction(_ sender:UIButton)  // added an "_ " before sender
{
    println("tapped button")
}
Run Code Online (Sandbox Code Playgroud)


小智 6

语法应为

@IBAction func buttonAction(_ sender: UIButton) {
     print('tapped')
 }
Run Code Online (Sandbox Code Playgroud)

语法是问题,还是

故事板上的按钮具有多个已注册的动作。要找出右键单击按钮,您应该能够看到有多少个连接

如果内部的修饰具有多个连接,则删除一个。这是因为如果您有多个连接,然后单击控件,则编译器会困惑于应实现哪个连接

  • 我的情况我在故事板中有 2 个注册的动作。 (2认同)

SPS*_*SPS 6

我知道现在回答这个问题为时已晚,但我正在为任何遇到类似问题的人回答这个问题。最近我有一个“无法识别的选择器发送到实例”问题,我花了一整天的时间来调试。所以我列出了这个问题的可能原因,希望这可以帮助遇到这个错误的人。

通常,当您将选择器传递给实例但该选择器不存在时,会引发此错误。简单来说,这就像有人给快递员一个地址,但该地址不存在。

如何调试: 1. 首先,检查所有@objc 标记的方法:当您使用#selector() 语法调用@objc 标记的方法时,方法签名应该完全相同。

  1. 当您对对象进行无效转换时,也会出现这种类型的错误。这是我的情况。我将属性字符串设置为标签,并在属性中传递了一个枚举大小写 (TextProperty.title) 而不是 (TextProperty.title.font)。所以字体变量返回一个 UIFont 对象。

由于属性中的键,字典期望“任何”Xcode 没有抛出错误。所以字体属性期待一个 UIFont 但得到了 TextProperty 枚举大小写。因此,请检查您是否在代码中进行了无效的转换。

  1. 您的代码中可能存在内存泄漏。例如:您可以将选择器发送到实例,但该选择器已被清除内存。

这些是我能想到的案例。如果你有什么可以帮助你的东西,欢迎提出建议。