简体   繁体   English

Swift中的“终止为NSException类型的未捕获异常”

[英]“terminating with uncaught exception of type NSException” in Swift

When I click on a button the app just crashes and I get this error: 当我单击一个按钮时,应用程序崩溃了,并出现以下错误:

2015-06-12 19:33:16.150 scroll view[79136:11092579] -[scroll_view.ViewController Action:]: unrecognized selector sent to instance 0x7f834148bdd0
2015-06-12 19:33:16.153 scroll view[79136:11092579] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[scroll_view.ViewController Action:]: unrecognized selector sent to instance 0x7f834148bdd0'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000103766c65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001052d1bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010376e0ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x00000001036c413c ___forwarding___ + 988
    4   CoreFoundation                      0x00000001036c3cd8 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x0000000104006da2 -[UIApplication sendAction:to:from:forEvent:] + 75
    6   UIKit                               0x000000010411854a -[UIControl _sendActionsForEvents:withEvent:] + 467
    7   UIKit                               0x0000000104117919 -[UIControl touchesEnded:withEvent:] + 522
    8   UIKit                               0x00000001043b1a10 _UIGestureRecognizerUpdate + 9487
    9   UIKit                               0x0000000104053686 -[UIWindow _sendGesturesForEvent:] + 1041
    10  UIKit                               0x00000001040542b2 -[UIWindow sendEvent:] + 666
    11  UIKit                               0x000000010401a581 -[UIApplication sendEvent:] + 246
    12  UIKit                               0x0000000104027d1c _UIApplicationHandleEventFromQueueEvent + 18265
    13  UIKit                               0x00000001040025dc _UIApplicationHandleEventQueue + 2066
    14  CoreFoundation                      0x000000010369a431 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    15  CoreFoundation                      0x00000001036902fd __CFRunLoopDoSources0 + 269
    16  CoreFoundation                      0x000000010368f934 __CFRunLoopRun + 868
    17  CoreFoundation                      0x000000010368f366 CFRunLoopRunSpecific + 470
    18  GraphicsServices                    0x000000010774da3e GSEventRunModal + 161
    19  UIKit                               0x0000000104005900 UIApplicationMain + 1282
    20  scroll view                         0x000000010356ac67 main + 135
    21  libdyld.dylib                       0x0000000105a29145 start + 1
    22  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Here is my code: 这是我的代码:

var imageView: UIImageView!
var scrollView: UIScrollView!
var containerView = UIView()
var buttonScroll: UIScrollView!


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.



    // 1
    imageView = UIImageView(image: UIImage(named: "Map 1.png"))
    // 2
    scrollView = UIScrollView(frame: view.bounds)
    scrollView.backgroundColor = UIColor.blackColor()
    // 3
    scrollView.contentSize = imageView.bounds.size
    // 4
    scrollView.addSubview(imageView)
    view.addSubview(scrollView)

    scrollView.contentSize.height = scrollView.frame.size.height

    var lvl1button  = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
    lvl1button.frame = CGRectMake(250, 140, 100, 50)
    let level1 = UIImage(named: "Lvl 1 Button.png") as UIImage!
    lvl1button.setImage(level1, forState: .Normal)
    lvl1button.addTarget(self, action: "Action:", forControlEvents: UIControlEvents.TouchUpInside)
    scrollView.addSubview(lvl1button)

You are setting up your button with an action selector called "Action:" . 您正在使用名为"Action:"的动作选择器来设置按钮。

lvl1button.addTarget(self, action: "Action:", forControlEvents: UIControlEvents.TouchUpInside)

But self , your view controller, has no such method so when Cocoa tries to call it on button-press, you crash. 但是self (您的视图控制器)没有这种方法,因此当Cocoa尝试在按下按钮时调用它时,您将崩溃。 Unfortunately you did not show that part of your code so one can only guess, but there could be a lot a reasons for this mismatch: the colon could be wrong, the spelling or capitalization could be wrong, and so on. 不幸的是,您没有显示代码的这一部分,因此只能猜测,但是这种不匹配的原因可能很多:冒号可能是错误的,拼写或大写字母可能是错误的,依此类推。 Or maybe you just forgot to write the method! 或者,也许您只是忘了编写方法!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift:使用未捕获的异常类型NSException终止应用程序 - Swift: Terminating app with uncaught exception type NSException Swift项目以NSException类型的未捕获异常终止 - Swift Project Terminating with uncaught exception of type NSException Swift:使用NSException类型的未捕获异常终止 - Swift: Terminating with uncaught exception of type NSException 终止类型为NSException的未捕获异常 - terminating uncaught exception of type NSException 以NSException 4类型的未捕获异常终止 - terminating with uncaught exception of type NSException 4 以NSException类型的未捕获异常终止 - terminating with uncaught exception of type NSException 以 NSException 类型的未捕获异常终止 - terminating with uncaught exception of type NSException SWIFT:使用Facebook登录时,“终止于NSException类型的未捕获异常” - SWIFT: “terminating with uncaught exception of type NSException” when sign in with facebook AppDelegate didFinishLaunchingWithOptions launchOptions-以NSException Swift 3.0类型的未捕获异常终止 - AppDelegate didFinishLaunchingWithOptions launchOptions - terminating with uncaught exception of type NSException Swift 3.0 使用类型NSException定时器Swift崩溃的未捕获异常终止 - Terminating With Uncaught Exception Of Type NSException Timer Swift Crash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM