【问题标题】:iOS app crashing during swipe gesture?iOS应用程序在滑动手势期间崩溃?
【发布时间】:2016-01-25 18:00:23
【问题描述】:

在我正在参加的开发者课程中练习滑动手势。据我所知,我已按照这些步骤操作,但似乎每次执行滑动(向右或向上)时,我的应用程序都会崩溃。

我已寻找解决方案,但似乎无法找出崩溃的原因。我已将“:”添加到我的操作中,并将 UIGestureRecognizerDelegate 添加到我的课程中。

应用程序会运行,但当我执行滑动时它会崩溃。
这是我的代码:

class ViewController: UIViewController, UIGestureRecognizerDelegate {

override func viewDidLoad() {
    super.viewDidLoad()

    var swipeRight = UISwipeGestureRecognizer(target: self, action: "swiped:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    self.view.addGestureRecognizer(swipeRight)

    var swipeUp = UISwipeGestureRecognizer(target: self, action: "swiped:")
    swipeUp.direction = UISwipeGestureRecognizerDirection.Up
    self.view.addGestureRecognizer(swipeUp)


    func swiped(gesture: UIGestureRecognizer) {

        if let swipeGesture = gesture as? UISwipeGestureRecognizer {

            switch swipeGesture.direction {

            case UISwipeGestureRecognizerDirection.Right:
                print("swiped right")
            case UISwipeGestureRecognizerDirection.Up:
                print("swiped up")
            default:
                break

            }

        }

    }



}

我收到的错误信息如下。

2015-10-26 10:59:52.944 Swipes and Shakes [2764:444281] -[Swipes_and_Shakes.ViewController swiped:]:无法识别的选择器发送到实例 0x7fea2a58bcd0 2015-10-26 10:59:52.950 Swipes and Shakes [2764:444281] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[Swipes_and_Shakes.ViewController 刷卡:]:无法识别的选择器发送到实例 0x7fea2a58bcd0” *** 首先抛出调用堆栈: ( 0 核心基础 0x000000010dfb2f45 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000010fccedeb objc_exception_throw + 48 2核心基础0x000000010dfbb56d-[NSObject(NSObject)不识别选择器:]+205 3 核心基础 0x000000010df08eea ___转发___ + 970 4 核心基础 0x000000010df08a98 _CF_forwarding_prep_0 + 120 5 UIKit 0x000000010ecae37c _UIGestureRecognizerSendTargetActions + 153 6 UIKit 0x000000010ecaacf6 _UIGestureRecognizerSendActions + 162 7 UIKit 0x000000010eca8cf3-[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 843 8 UIKit 0x000000010ecb0c9f ___UIGestureRecognizerUpdate_block_invoke877 + 79 9 UIKit 0x000000010ecb0b3d _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 342 10 UIKit 0x000000010ec9edb1 _UIGestureRecognizerUpdate + 2634 11 UIKit 0x000000010e840684-[UIWindow _sendGesturesForEvent:] + 1137 12 UIKit 0x000000010e8418ba -[UIWindow 发送事件:] + 849 13 UIKit 0x000000010e7f101a -[UIApplication 发送事件:] + 263 14 UIKit 0x000000010e7cb8c7 _UIApplicationHandleEventQueue + 6844 15 核心基础 0x000000010dedf011 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 16 核心基础 0x000000010ded4f3c __CFRunLoopDoSources0 + 556 17 核心基础 0x000000010ded43f3 __CFRunLoopRun + 867 18 核心基础 0x000000010ded3e08 CFRunLoopRunSpecific + 488 19 图形服务 0x00000001125d7ad2 GSEventRunModal + 161 20 UIKit 0x000000010e7d1031 UIApplicationMain + 171 21 次滑动和摇晃 0x000000010ddd326d 主要 + 109 22 libdyld.dylib 0x00000001107d092d 开始 + 1 ) libc++abi.dylib:以 NSException 类型的未捕获异常终止 (lldb)

【问题讨论】:

  • 你不能把你的函数声明放在另一个函数里面。
  • 错误消息是:“未捕获的异常 'NSInvalidArgumentException',原因:'-[Swipes_and_Shakes.ViewController swiped:]: unrecognized selector sent”,这准确地解释了错误。此外,Simulatoe 似乎不仅仅让您的应用程序在模拟器中崩溃,这是一个主要区别。
  • 实际上在 Swift 中你可以将一个函数放入另一个函数中。
  • 您可以编辑您的问题,发布您正在使用的正确代码吗?
  • func swiped(gesture: UIGestureRecognizer) 在函数 override func viewDidLoad() 内部。看看呃大括号和缩进。修复它。

标签: ios swift unrecognized-selector uiswipegesturerecognizer


【解决方案1】:

声明在 viewDidLoad 外刷过并且它不应该崩溃

class ViewController: UIViewController, UIGestureRecognizerDelegate {

override func viewDidLoad() {
    super.viewDidLoad()

    var swipeRight = UISwipeGestureRecognizer(target: self, action: "swiped:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    self.view.addGestureRecognizer(swipeRight)

    var swipeUp = UISwipeGestureRecognizer(target: self, action: "swiped:")
    swipeUp.direction = UISwipeGestureRecognizerDirection.Up
    self.view.addGestureRecognizer(swipeUp)
}

func swiped(gesture: UIGestureRecognizer) {

    if let swipeGesture = gesture as? UISwipeGestureRecognizer {

        switch swipeGesture.direction {

        case UISwipeGestureRecognizerDirection.Right:
            print("swiped right")
        case UISwipeGestureRecognizerDirection.Up:
            print("swiped up")
        default:
            break

        }

    }

}

【讨论】:

  • 就这么简单。非常感谢你们。
【解决方案2】:

您必须在视图加载函数之外声明您的函数。试试看。

【讨论】:

    猜你喜欢
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多