当我使用 UISwipeGestureRecognizer 时,我得到 Thread 1:Signal SIGABRT
When I'm using UISwipeGestureRecognizer I'm getting Thread 1:Signal SIGABRT
我完全把代码写成了用于培训目的的 youtube 视频教程。这是我制作的第二个有同样错误的程序。我的 iOS 模拟器有问题吗?
代码如下:
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-08-03 15:13:09.545 SwipeGestures[14088:457164]
-[SwipeGestures.ViewController handleSwipes]: unrecognized selector sent to instance 0x79f92120 2015-08-03 15:13:09.550
SwipeGestures[14088:457164] * Terminating app due to uncaught
exception 'NSInvalidArgumentException', reason:
'-[SwipeGestures.ViewController handleSwipes]: unrecognized selector
sent to instance 0x79f92120'
* First throw call stack: ( 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 _UIGestureRecognizerSendActions + 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 main + 43 26 libdyld.dylib
0x02406ac9 start + 1 ) libc++abi.dylib: terminating with uncaught
exception of type NSException (lldb)
在方法名称后添加一个冒号:
Selector("handleSwipes:")
这是因为handleSwipes在其定义中有一个参数。
将您的手势代码替换为以下代码,
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:")
尝试为 UISwipeGestureRecognizer 分配委托。例如:
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 个。
您忘记添加代理人:
import UIKit
class ViewController: UIViewController, UIGestureRecognizerDelegate {
我完全把代码写成了用于培训目的的 youtube 视频教程。这是我制作的第二个有同样错误的程序。我的 iOS 模拟器有问题吗?
代码如下:
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-08-03 15:13:09.545 SwipeGestures[14088:457164] -[SwipeGestures.ViewController handleSwipes]: unrecognized selector sent to instance 0x79f92120 2015-08-03 15:13:09.550 SwipeGestures[14088:457164] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SwipeGestures.ViewController handleSwipes]: unrecognized selector sent to instance 0x79f92120' * First throw call stack: ( 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 _UIGestureRecognizerSendActions + 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 main + 43 26 libdyld.dylib
0x02406ac9 start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)
在方法名称后添加一个冒号:
Selector("handleSwipes:")
这是因为handleSwipes在其定义中有一个参数。
将您的手势代码替换为以下代码,
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:")
尝试为 UISwipeGestureRecognizer 分配委托。例如:
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 个。
您忘记添加代理人:
import UIKit
class ViewController: UIViewController, UIGestureRecognizerDelegate {