【问题标题】:Getting 'NSInvalidArgumentException' error while using 'endEditing'使用“endEditing”时出现“NSInvalidArgumentException”错误
【发布时间】:2020-03-08 13:16:03
【问题描述】:

我收到此错误:

[Clima.WeatherViewController searchPressed:]:无法识别的选择器发送到实例 0x7f996b204020 *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[Clima.WeatherViewController searchPressed:]:无法识别的选择器发送到实例 0x7f996b204020”

这是我的代码:

IBOutlet weak var searchField: UITextField! 
IBAction func searchedButtonPressed(_ sender: UIButton) { 
         searchField.endEditing(true);
         print(searchField.text!) 
}

【问题讨论】:

  • IBOutlet 弱变量 searchField:UITextField! IBAction func searchedButtonPressed(_ sender: UIButton) { searchField.endEditing(true);打印(searchField.text!)}
  • 欢迎来到 Stack Overflow。请使用tour 并查看How to Ask。从错误消息中看起来很明显。您的代码在没有 searchPressed 函数的视图控制器上调用 searchPressed
  • 选择您的按钮并查看Connections Inspector 窗格。我猜您的按钮与searchPressed 有关联,但您将代码中的函数重命名为searchedButtonPressed

标签: ios swift


【解决方案1】:

您只需断开按钮的 IBAction 并重新连接即可。因为您似乎在连接后更改了 IBAction 函数名称。因此,您的故事板仍然与旧名称 search Pressed 有联系。 & 您已将其重命名为 searchedButtonPressed,因此您的故事板找不到这个新名称。

断开您的 IBAction 方法:

右键单击故事板中的搜索按钮,您将看到您在 manu 中建立的连接,使用位于角落顶部的名为 searchPressed 的小 colse 按钮​​关闭连接。

重新连接您的 IBAction 方法:

按住键盘上的 Ctrl 键并在搜索按钮上拖动到您将在 IBAction 方法旁边看到的小圆圈。

【讨论】:

    猜你喜欢
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 2013-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多