2015-06-13 323 views
0

「型NSException的未捕獲的異常終止」當我點擊一個按鈕的應用程序只崩潰,我得到這個錯誤:斯威夫特

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 

這裏是我的代碼:

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) 
+0

你有一個名爲'Action'的方法嗎? –

回答

2

你正在設置一個名爲"Action:"的動作選擇器。

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

self,您的視圖控制器,有沒有這樣的方法,所以當可可嘗試調用它後的按鍵,你崩潰。不幸的是,你沒有顯示你的代碼的一部分,所以只能猜測,但可能有很多原因造成這種不匹配:冒號可能是錯的,拼寫或大寫可能是錯誤的,等等。或者你可能忘了寫這個方法!

+0

或者考慮到它的通用性,可能只是簡單地複製並粘貼這行並不完全理解它的功能。 – nhgrif

+0

通常,當我看到一行我不熟悉的代碼時,我點擊它,它會顯示正在傳遞的函數及其參數。我建議OP應該這樣做。 – DDPWNAGE