swift - 如何检索 switch 的值并将其传递给 Object?

标签 swift uiswitch unrecognized-selector

我正在尝试获取开关的值并将其传递给 View Controller 中的对象。

下面是我的 View Controller 的代码。我创建了标签、开关 socket 和开关操作。

@IBOutlet weak var firstTimeConducting: UILabel!
@IBOutlet weak var firstTime_Conduct: UISwitch!

@IBAction func firstTimeConductSwitchTap(_ sender: Any) {
    firstTimeConductTapped()
}

override func viewDidLoad() {
    super.viewDidLoad()
    firstTimeConductTapped()
}

func firstTimeConductTapped() {
    if firstTime_Conduct.isOn {
        value = "Yes"
    } else {
        value = "No"
    }
}

我已经对其进行了调试,当我单击模拟器中从关闭到开启状态的开关时,它进入并获取值为“yes”。

然后返回函数firstTimeConductSwitchTap()后,它崩溃并显示以下错误

"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[app.RequestFormViewController first_TimeConduct:]: unrecognized selector sent to instance 0x7fb900421fc0'

我只是检索“value”变量的值并将其传递给要显示的对象。

您能否建议我是否遵循了正确的程序以及为什么会出现此错误?

最佳答案

错误非常明显:

在 Interface Builder 中,与选择器 first_TimeConduct 存在死连接。您很可能重命名了操作方法。

使用 ⇧⌘F 搜索 first_TimeConduct 并删除连接。

关于swift - 如何检索 switch 的值并将其传递给 Object?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53592230/

相关文章:

ios - 在启动手机应用程序之前,在 Watch 上从 iPhone 获取字典 - WC

Android Switch Button Drag off 点击/拖动

ios - 在重用单元格并设置 UISwitch 是否处于事件状态时,ios listview 将第一行和最后一行设置为事件状态,有什么问题?

ios - 无法识别的选择器发送到实例 0x7f90f1f04180

ios - 试图在 swift 中检测 iphone x 总是返回 false

iOS 高 rebase /绑定(bind)时间

ios - 不能对 PFObject 上的键或值使用 nil。使用 NSNull 作为值

ios - 在侧面菜单中以编程方式快速添加开关

ios - 如何向 subview 属性 Swift 添加操作?

swift :"Unrecognized selector sent to instance",Xcode 9.4.1