Xcode-按下按钮时,以NSException类型的未捕获异常终止
我是应用程序开发新手,我正在跟随苹果学习。我已经看过很多关于这类错误的问题,但是没有一个能帮到我。在本教程中,我一直停留在“将按钮添加到视图”部分,在该部分中,我试图实现一个字符串,当点击按钮时,该字符串将在控制台上打印出来。只有我这样做时会出错 Swift中按钮的我的代码:Xcode-按下按钮时,以NSException类型的未捕获异常终止,xcode,swift,button,nsexception,Xcode,Swift,Button,Nsexception,我是应用程序开发新手,我正在跟随苹果学习。我已经看过很多关于这类错误的问题,但是没有一个能帮到我。在本教程中,我一直停留在“将按钮添加到视图”部分,在该部分中,我试图实现一个字符串,当点击按钮时,该字符串将在控制台上打印出来。只有我这样做时会出错 Swift中按钮的我的代码: import UIKit class StarRatingControl: UIView { // MARK: Initialization required init?(coder aDecoder
import UIKit
class StarRatingControl: UIView {
// MARK: Initialization
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
button.backgroundColor = UIColor.cyanColor()
button.addTarget(self, action: "ratingButtonTapped", forControlEvents: .TouchDown)
addSubview(button)
}
override func intrinsicContentSize() -> CGSize {
return CGSize(width: 240, height: 44)
}
// MARK: Button Action
func ratingButtonTapped(button: UIButton) {
print("Button pressed!")
}
}
2015-09-16 12:43:17.409 FoodTracker[954:13341] -[FoodTracker.StarRatingControl ratingButtonTapped]: unrecognized selector sent to instance 0x7f939b7a43b0
2015-09-16 12:43:17.443 FoodTracker[954:13341] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FoodTracker.StarRatingControl ratingButtonTapped]: unrecognized selector sent to instance 0x7f939b7a43b0'
*** First throw call stack:
(
0 CoreFoundation 0x00000001079f59b5 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001095f4deb objc_exception_throw + 48
2 CoreFoundation 0x00000001079fdfdd -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010794b9fa ___forwarding___ + 970
4 CoreFoundation 0x000000010794b5a8 _CF_forwarding_prep_0 + 120
5 UIKit 0x0000000108214522 -[UIApplication sendAction:to:from:forEvent:] + 92
6 UIKit 0x0000000108373c06 -[UIControl sendAction:to:forEvent:] + 67
7 UIKit 0x0000000108373eac -[UIControl _sendActionsForEvents:withEvent:] + 273
8 UIKit 0x0000000108372b1c -[UIControl touchesBegan:withEvent:] + 261
9 UIKit 0x000000010827bf50 -[UIWindow _sendTouchesForEvent:] + 308
10 UIKit 0x000000010827cd4d -[UIWindow sendEvent:] + 865
11 UIKit 0x00000001082312ae -[UIApplication sendEvent:] + 263
12 UIKit 0x000000010820d36c _UIApplicationHandleEventQueue + 6693
13 CoreFoundation 0x0000000107921b21 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
14 CoreFoundation 0x0000000107917a4c __CFRunLoopDoSources0 + 556
15 CoreFoundation 0x0000000107916f03 __CFRunLoopRun + 867
16 CoreFoundation 0x0000000107916918 CFRunLoopRunSpecific + 488
17 GraphicsServices 0x000000010befead2 GSEventRunModal + 161
18 UIKit 0x000000010821299e UIApplicationMain + 171
19 FoodTracker 0x00000001078118dd main + 109
20 libdyld.dylib 0x000000010a11392d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
错误:
import UIKit
class StarRatingControl: UIView {
// MARK: Initialization
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
button.backgroundColor = UIColor.cyanColor()
button.addTarget(self, action: "ratingButtonTapped", forControlEvents: .TouchDown)
addSubview(button)
}
override func intrinsicContentSize() -> CGSize {
return CGSize(width: 240, height: 44)
}
// MARK: Button Action
func ratingButtonTapped(button: UIButton) {
print("Button pressed!")
}
}
2015-09-16 12:43:17.409 FoodTracker[954:13341] -[FoodTracker.StarRatingControl ratingButtonTapped]: unrecognized selector sent to instance 0x7f939b7a43b0
2015-09-16 12:43:17.443 FoodTracker[954:13341] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FoodTracker.StarRatingControl ratingButtonTapped]: unrecognized selector sent to instance 0x7f939b7a43b0'
*** First throw call stack:
(
0 CoreFoundation 0x00000001079f59b5 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001095f4deb objc_exception_throw + 48
2 CoreFoundation 0x00000001079fdfdd -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010794b9fa ___forwarding___ + 970
4 CoreFoundation 0x000000010794b5a8 _CF_forwarding_prep_0 + 120
5 UIKit 0x0000000108214522 -[UIApplication sendAction:to:from:forEvent:] + 92
6 UIKit 0x0000000108373c06 -[UIControl sendAction:to:forEvent:] + 67
7 UIKit 0x0000000108373eac -[UIControl _sendActionsForEvents:withEvent:] + 273
8 UIKit 0x0000000108372b1c -[UIControl touchesBegan:withEvent:] + 261
9 UIKit 0x000000010827bf50 -[UIWindow _sendTouchesForEvent:] + 308
10 UIKit 0x000000010827cd4d -[UIWindow sendEvent:] + 865
11 UIKit 0x00000001082312ae -[UIApplication sendEvent:] + 263
12 UIKit 0x000000010820d36c _UIApplicationHandleEventQueue + 6693
13 CoreFoundation 0x0000000107921b21 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
14 CoreFoundation 0x0000000107917a4c __CFRunLoopDoSources0 + 556
15 CoreFoundation 0x0000000107916f03 __CFRunLoopRun + 867
16 CoreFoundation 0x0000000107916918 CFRunLoopRunSpecific + 488
17 GraphicsServices 0x000000010befead2 GSEventRunModal + 161
18 UIKit 0x000000010821299e UIApplicationMain + 171
19 FoodTracker 0x00000001078118dd main + 109
20 libdyld.dylib 0x000000010a11392d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
我知道Xcode保留了对我已删除的操作/出口的旧引用,所以我尝试右键单击Interface Builder中的视图控制器以查找旁边带有感叹号的项目,但找不到任何内容(到目前为止,我所知道的唯一故障排除方法)。我使用的是Xcode beta 7.0。感谢您的帮助 您缺少选择器中的:“
,请尝试以下操作:
button.addTarget(self, action: "ratingButtonTapped:", forControlEvents: .TouchDown)
我希望这对你有帮助。我也有同样的问题,
检查Xcode的版本
#选择器附带Xcode 7.3
如果您有较旧的版本更新,这可能会修复您的错误
问候,,
Driss如果您使用的是Swift 3,则相关行必须更改为:
button.addTarget(self,action:#selector(StarRatingControl.ratingbuttonattapped),for:。着地)
为了参考,我决定查看一下示例,并发现了类似的问题
我使用的是XCode 8+Swift 3,为了使其正常工作,我做了以下更改:
button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped), for: .touchDown)
注:考虑文件名“RatingControl.swift”
我希望它能帮上忙,谢谢你的帮助!我不敢相信我在文件里漏掉了。不幸的是,我不知道它是否对我有帮助,因为我做了一些导致另一个错误(oops)的事情。我永久删除了一个名为RatingControl.swift的文件。我创建了另一个名为StarRatingControl.swift的swift文件,试图排除故障,并将按钮代码复制到该文件中(您在本主题中看到的代码)。当我试着用你的修正运行模拟器时,我在你提到的那行上得到一个错误,上面写着“线程1:断点1.1”,控制台上只打印了“(lldb)”。我想知道你是否也能帮我解决这个问题。