Swift | UIButton 导致点击时崩溃
Swift | UIButton causes crash on tap
我在应用程序的主屏幕上添加了一个按钮,点击一个按钮,就会出现一个新的 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'
您可以将点击手势识别器添加到按钮本身。最佳做法是使用 outlet,但这很好用并且对其他 UI 组件(如视图或标签)也很有用
let loginTapGesture = UITapGestureRecognizer(target: self,
action: #selector(loginButtonTapped1))
loginButton.addGestureRecognizer(loginTapGesture)
答案在错误消息中。某处,我猜是在 LoginViewController
中,有一个 LoginView
类型的视图。该视图正在调用 addTarget(_:action:for:)
。 LoginView
不是 UIControl
的子class,也没有 addTarget(_:action:for:)
。它导致了崩溃。
让我分解一下 -[SparkGPS.LoginView addTarget:action:forControlEvents:]
的部分。
开头的-
表示它是一个实例方法而不是静态方法或class方法。
SparkGPS.LoginView
是模块,class。模块是框架或应用程序的另一种说法。在这种情况下,您似乎有一个名为 SparkGPS
的应用程序和一个名为 LoginView
.
的 class
addTarget:action:forControlEvents:
是 Objective-C 对 addTarget(_:action:for:)
的称呼。
最后,"selector sent to instance"表示变量调用方法。选择器是一种标识方法的方式,实例存储在变量中。例如,在您的代码中有 loginButton.setTitle("Login", for: .normal)
。这可以表述为 setTitle(_:for:)
被发送到实例 loginButton
.
我在应用程序的主屏幕上添加了一个按钮,点击一个按钮,就会出现一个新的 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'
您可以将点击手势识别器添加到按钮本身。最佳做法是使用 outlet,但这很好用并且对其他 UI 组件(如视图或标签)也很有用
let loginTapGesture = UITapGestureRecognizer(target: self,
action: #selector(loginButtonTapped1))
loginButton.addGestureRecognizer(loginTapGesture)
答案在错误消息中。某处,我猜是在 LoginViewController
中,有一个 LoginView
类型的视图。该视图正在调用 addTarget(_:action:for:)
。 LoginView
不是 UIControl
的子class,也没有 addTarget(_:action:for:)
。它导致了崩溃。
让我分解一下 -[SparkGPS.LoginView addTarget:action:forControlEvents:]
的部分。
开头的
-
表示它是一个实例方法而不是静态方法或class方法。SparkGPS.LoginView
是模块,class。模块是框架或应用程序的另一种说法。在这种情况下,您似乎有一个名为SparkGPS
的应用程序和一个名为LoginView
. 的 class
addTarget:action:forControlEvents:
是 Objective-C 对addTarget(_:action:for:)
的称呼。
最后,"selector sent to instance"表示变量调用方法。选择器是一种标识方法的方式,实例存储在变量中。例如,在您的代码中有 loginButton.setTitle("Login", for: .normal)
。这可以表述为 setTitle(_:for:)
被发送到实例 loginButton
.