gpt4 book ai didi

ios - 将 Traget 添加到 UIView 中的 UIButton。收到错误 "terminating with uncaught exception of type NSException"

转载 作者:搜寻专家 更新时间:2023-11-01 06:55:53 25 4
gpt4 key购买 nike

我有一个 UIView 类,里面有一个 UIButton,单击它时应该会打开一个 UIViewController 类。但是,每次我单击它时,我都会在 AppDelegate - Thread 1: signal SIGABRT 中收到错误消息。终端:以 NSException 类型的未捕获异常终止。

lazy var signupButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Sign me up", for: .normal)
button.setTitleColor(UIColor.blue, for: .normal)
button.backgroundColor = UIColor.white
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: #selector(handleLogin(vc:)), for: .touchUpInside)
return button
}()

@objc func handleLogin(vc: UIViewController) {
let loginController = LoginController()
vc.present(loginController, animated: true, completion: nil)
}

最佳答案

问题出在 func handleLogin 的参数上。您将其设置为期望 UIViewController 的实例,但它实际上是选择器的发送者,在本例中是 UIButton

您需要将其更新为以下内容:

@objc func handleLogin(sender: UIButton) {
let loginController = LoginController()
present(loginController, animated: true, completion: nil)
}

您可以交替执行此操作,因为您根本不需要 sender 参数。

button.addTarget(self, action: #selector(handleLogin), for: .touchUpInside)
return button

@objc func handleLogin() {
let loginController = LoginController()
present(loginController, animated: true, completion: nil)
}

关于ios - 将 Traget 添加到 UIView 中的 UIButton。收到错误 "terminating with uncaught exception of type NSException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53419047/

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