gpt4 book ai didi

Swift: "unrecognised selector sent to instance"有/没有委托(delegate)

转载 作者:行者123 更新时间:2023-11-30 13:03:59 26 4
gpt4 key购买 nike

** 下面描述的所有内容都是以编程方式完成的。不使用 Storyboard **

在我的 ViewController 中,我创建了一个包含按钮和标签的 CustomCheckBoxContainer。 (此处未显示标签!!)我已向按钮添加了操作,只要我处理子级中的函数(例如 CustomCheckBoxContainer 和 CustomCheckBox),天气就会闪闪发亮。但是,当尝试添加调用 ViewController 中的函数的操作时,我的项目上方开始形成云。

错误消息:*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UIView buttonClicked3:]:无法识别的选择器发送到实例...”。

按钮操作:

newButton.addTarget(currentView, action: #selector(ViewController.buttonClicked3(_:)), forControlEvents: UIControlEvents.TouchUpInside)

然后意识到您无法将数据或操作发送回上游。因此创建了协议(protocol)和委托(delegate)并将操作重写为:

newButton.addTarget(self, action: #selector(delegate3.buttonClicked3(_:)), forControlEvents: UIControlEvents.TouchUpInside)

。不幸的是,结果完全相同。

我完全被困住了。我没看到什么?

ViewController.swift

class ViewController: UIViewController, ButtonClicked3Delegate  {

@objc func buttonClicked3(sender: UIButton) {
print ("Action received in the ViewController Class")
}

let userArray: [String] = ["one","two”]

override func viewDidLoad() {
super.viewDidLoad()

for item in userArray {
let customCheckBoxContainer = CustomCheckBoxContainer()
customCheckBoxContainer.showNewButton(..args.., currentView: UIView, ... args ...)
self.view.addSubview(customCheckBoxContainer)
}
}
}

CustomCheckBoxContainer.swift:

    @objc protocol ButtonClicked3Delegate {
@objc func buttonClicked3(sender: UIButton)
//other data needed
}

class CustomCheckBoxContainer: UIView {
var newButton: CustomCheckBox!

var delegate3 : ButtonClicked3Delegate!

func showNewButton (... args... currentView: UIView, ... args ... ) {
newButton = CustomCheckBox (type: UIButtonType.Custom)
newButton.bounds = ...
newButton.center = ...

newButton.addTarget(newButton, action: #selector(CustomCheckBox.buttonClicked1(_:)), forControlEvents: UIControlEvents.TouchUpInside)
newButton.addTarget(self, action: #selector(CustomCheckBoxContainer.buttonClicked2(_:)), forControlEvents: UIControlEvents.TouchUpInside)
newButton.addTarget(self, action: #selector(delegate3.buttonClicked3(_:)), forControlEvents: UIControlEvents.TouchUpInside)

currentView.addSubview(newButton)

}

func buttonClicked2(sender:UIButton) {
print ("Action received in the CustomCheckBoxContainer Class")
}
}

自定义CheckBox.swift

class CustomCheckBox: UIButton {

func buttonClicked1(sender:UIButton) {
print ("Action was received in CustomCheckBox Class")
}
}

最佳答案

我会将其分为两个阶段。

按照以下几行将按钮链接到 subview Controller 中的选择器(以下是 Swift 2.3 语法):

button.addTarget(self, action #selector(self.buttonTapped()), forControlEvents: UIControlEvents.touchUpInside)

然后从您的 ButtonTapped(_:) 实现中调用委托(delegate)方法:

func buttonTapped() -> Void {
Self.delegate.buttonWasTapped()
...
}

[顺便说一下,还要检查委托(delegate)是否不为零。您已将其声明为隐式展开的可选,这是正确的,但这意味着您需要从父 View Controller 设置委托(delegate)。]

关于Swift: "unrecognised selector sent to instance"有/没有委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39603684/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com