2015-08-03 200 views
0

我正好寫了代碼作爲youtube視頻教程的培訓目的。這是我用同樣的錯誤做的第二個程序。我的iOS模擬器有問題嗎?當我使用UISwipeGestureRecognizer時,我得到的線程1:信號SIGABRT

這裏是代碼:

import UIKit 

class ViewController: UIViewController { 

@IBOutlet weak var Label: UILabel! 
override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    var leftSwipe = UISwipeGestureRecognizer(target:self,action: Selector("handleSwipes")) 
    var rightSwipe = UISwipeGestureRecognizer(target:self,action: Selector("handleSwipes")) 
    var upSwipe = UISwipeGestureRecognizer(target:self,action: Selector("handleSwipes")) 
    var downSwipe = UISwipeGestureRecognizer(target:self,action: Selector("handleSwipes")) 

    leftSwipe.direction = .Left 
    rightSwipe.direction = .Right 
    upSwipe.direction = .Up 
    downSwipe.direction = .Down 

    view.addGestureRecognizer(leftSwipe) 
    view.addGestureRecognizer(rightSwipe) 
    view.addGestureRecognizer(upSwipe) 
    view.addGestureRecognizer(downSwipe) 


} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


func handleSwipes(sender:UISwipeGestureRecognizer){ 

    if (sender.direction == .Left){ 
     Label.text = "Left" 
    } 
    if (sender.direction == .Right){ 
     Label.text = "Right" 
    } 
    if (sender.direction == .Up){ 
     Label.text = "Up" 
    } 
    if (sender.direction == .Down){ 
     Label.text = "Down" 
    } 
} 

}

錯誤是:

2015年8月3日15:13:09.545 SwipeGestures [14088:457164] - [ SwipeGestures.ViewController handleSwipes]:無法識別的選擇器發送到實例0x79f92120 2015-08-03 15:13:09.550 SwipeGestures [14088:457164] *終止應用程序由於unca ught 異常 'NSInvalidArgumentException',原因: ' - [SwipeGestures.ViewController handleSwipes]:發送到實例0x79f92120無法識別選擇 ' *第一擲調用堆棧:(0的CoreFoundation 0x002a9466 exceptionPreprocess + 182 1 libobjc.A.dylib
0x01c98a97 objc_exception_throw + 44 2的CoreFoundation
0x002b12c5 - [NSObject的(NSObject的)doesNotRecognizeSelector:] + 277 3
的CoreFoundation 0x001f9bc7 ___forwarding_
+ 1047 4的CoreFoundation 0x001f978e _CF_forwarding_prep_0 + 14 5的UIKit 0x00f05057 _UIGestureRecognizer SendActions + 327 6的UIKit
0x00f038d4 - [UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 561 7的UIKit 0x00f0591d - [UIGestureRecognizer _delayedUpdateGesture] + 60 8的UIKit 0x00f0929a _UIGestureRecognizerUpdate_block_invoke661 + 57 9
的UIKit 0x00f0915d _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 317 10的UIKit 0x00efd066 _UIGestureRecognizerUpdate + 3720 11的UIKit 0x00b14c5b - [一個UIWindow _sendGesturesForEvent:] + 1356 12的UIKit
0x00b15abf - [一個UIWindow的SendEvent:] + 769 13的UIKit
0x00adabb1 - [UIApplication的的SendEvent:] + 242 14的UIKit
0x00aeabf6 _UIApplicationHandleEventFromQueueEvent + 21066 15的UIKit 0x00abebc7 _UIApplicationHandleEventQueue + 2300 16的CoreFoundation
0x001cc98f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION
+ 15 17的CoreFoundation 0x001c249d __CFRunLoopDoSources0 + 253 18的CoreFoundation 0x001c19f8 __CFRunLoopRun + 952 19的CoreFoundation
0x001c137b CFRunLoopRunSpecific + 443 20的CoreFoundation
0x001c11ab CFRunLoopRunInMode + 123 21 GraphicsServices
0x0404e2c1 GSEventRunModal + 192 22 GraphicsServices
0x0404e0fe GSEventRun + 104 23 UIKit
個0x00ac29b6 UIApplicationMain + 1526 24 SwipeGestures
0x000c2e1e top_level_code + 78個25 SwipeGestures
0x000c2e5b主+ 43 26 libdyld.dylib
0x02406ac9啓動+ 1)的libC++ ABI。dylib:與未捕獲 異常類型NSException(LLDB)的終止

+0

感謝。我是新來的! – Adham

回答

1

添加一個冒號之後方法的名稱:

Selector("handleSwipes:")

這是因爲handleSwipes在其定義的參數。

+0

謝謝。這對我有用! – Adham

+0

只要我被允許...還不到15歲,她就會回覆你的答案! – Adham

3

下面一個替換你的動作代碼,

var leftSwipe = UISwipeGestureRecognizer(target: self, action: "handleSwipes:") 
    var rightSwipe = UISwipeGestureRecognizer(target: self, action: "handleSwipes:") 
    var upSwipe = UISwipeGestureRecognizer(target: self, action: "handleSwipes:") 
    var downSwipe = UISwipeGestureRecognizer(target: self, action: "handleSwipes:") 
+0

謝謝,這工作! – Adham

+0

不謝謝.. ..接受/ upvote的答案.. –

+0

即時消息不被允許連根拔起,因爲我有少於15個聲望點 – Adham

1

嘗試分配用於UISwipeGestureRecognizer委託。 e.g:

class ViewController: UIViewController, UIGestureRecognizerDelegate { 

和:

leftSwipe.delegate = self 

並實行:

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool { 
    return true 
} 

如果還是失敗,在選擇名稱添加冒號別人的建議。

如果您使用以下教程:http://www.raywenderlich.com/76020/using-uigesturerecognizer-with-swift-tutorial您還可以閱讀,如果您有多個手勢識別器,則需要該代表。默認情況下只允許1個。

+0

是的將在未來的人使用它...病態upvote你的答案一旦我被允許。 ..還不到15! – Adham

2

你忘了添加一個委託:

import UIKit 

    class ViewController: UIViewController, UIGestureRecognizerDelegate { 
相關問題