ios - swift | UIButton 导致点击时崩溃

标签 ios swift uinavigationcontroller uibutton presentviewcontroller

我在应用程序的主屏幕上添加了一个按钮,点击一个按钮,就会出现一个新的 viewcontroller。 这在模拟器中工作得很好,但当我在实际的 iPhone 中尝试时,它会导致应用程序崩溃。

此外,崩溃仅在登录按钮上引起,而以相同方式制作的注册按钮却完美无缺

我会在下面留下代码

 var loginButton = UIButton()
var signUpButton = UIButton()

 loginButton.setTitle("Login", for: .normal)
    loginButton.titleLabel?.textAlignment = .center
    loginButton.backgroundColor = appGreenTheme
    loginButton.titleLabel?.textColor = .white
    loginButton.layer.cornerRadius = 20
    loginButton.titleLabel?.font = UIFont.systemFont(ofSize: 20)
    loginButton.setBackgroundImage(UIImage(named: "pinkOrangeGradientPDF"), for: .normal)
    loginButton.clipsToBounds = true


    signUpButton.setTitle("Sign Up", for: .normal)
    signUpButton.setTitleColor(.black, for: .normal)
    signUpButton.titleLabel?.textAlignment = .center
    signUpButton.backgroundColor = .white
    signUpButton.titleLabel?.textColor = .black
    signUpButton.layer.cornerRadius = 20
    signUpButton.titleLabel?.font = UIFont.systemFont(ofSize: 20)


    loginButton.addTarget(self, action: #selector(loginButtonTapped1(_:)), for: .allTouchEvents)

    signUpButton.addTarget(self, action: #selector(signUpButtonTapped1(_:)), for: .allTouchEvents)

 ///////////////////////////////////////////////////

   @objc func loginButtonTapped1(_ sender: UIButton) {
    let nav = UINavigationController(rootViewController: LoginViewController())
self.present(nav, animated: true, completion: nil)
}

@objc func signUpButtonTapped1(_ sender: UIButton) {
    let nav = UINavigationController(rootViewController: SignUpViewController())
    self.present(nav, animated: true, completion: nil)
}

我还尝试了“touchUpInside”事件。它再次在模拟器中完美运行,但在物理设备中运行不佳。

欢迎任何帮助。

下面是日志中显示的错误

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SparkGPS.LoginView addTarget:action:forControlEvents:]: unrecognized selector sent to instance 0x13dd4c740'

最佳答案

答案在错误消息中。某处,我猜是在 LoginViewController 中,有一个 LoginView 类型的 View 。该 View 正在调用 addTarget(_:action:for:)LoginView 不是 UIControl 的子类,也没有 addTarget(_:action:for:)。它导致了崩溃。

让我分解一下 -[SparkGPS.LoginView addTarget:action:forControlEvents:] 的部分。

  • 开头的-表示它是一个实例方法,而不是静态方法或类方法。

  • SparkGPS.LoginView 是模块和类。模块是框架或应用程序的另一种说法。在这种情况下,看起来您有一个名为 SparkGPS 的应用和一个名为 LoginView 的类。

  • addTarget:action:forControlEvents: 是 Objective-C 对 addTarget(_:action:for:) 的名称。

最后,“selector sent to instance”表示变量调用一个方法。选择器是一种标识方法的方式,实例存储在变量中。例如,在您的代码中有 loginButton.setTitle("Login", for: .normal)。这可以表述为 setTitle(_:for:) 被发送到实例 loginButton

关于ios - swift | UIButton 导致点击时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55843887/

相关文章:

ios - 当我更改 ViewController 时 UIImageView 不旋转

swift - 有没有办法在 switch 语句中将变量绑定(bind)到多个替代项?

ios - 在启动时从导航堆栈中实例化 View Controller

ios - 核心数据最佳实践——我在利用 C.D.正确有效? - iOS swift

ios - 从 Metal 内核写入 32 位浮点纹理?文档说是,运行时说不

ios - 在自定义 UIView 中动画化 UIBezierPath 不起作用

ios - UIWebView 有 UIToolbar 吗?

iphone - 如何使 UINavigationController 的 Navigation Bar 显示在底部?

ios - 过滤器在 GPUImageBulgeDistortionFilter 中应用不正确

iphone - UINavigationBar titleView 的最大尺寸是多少?