【问题标题】:Swift 4 Unrecognized Selector setting up a gesture recognizerSwift 4 Unrecognized Selector 设置手势识别器
【发布时间】:2018-11-05 18:01:18
【问题描述】:

我正在为表格视图单元格设置UILongPressGestureRecognizer,并且在尝试测试手势时在运行时遇到以下异常:

2018-05-26 10:10:21.740077-0500 Rumble Ios[43135:916767]-[UIButton longPress]:无法识别的选择器发送到实例 0x7fea34e210c0 2018-05-26 10:10:21.756166-0500 Rumble Ios [43135:916767] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UIButton longPress]:无法识别的选择器发送到实例 0x7fea34e210c0” *** 首先抛出调用堆栈: ( 0 核心基础 0x000000010a76e1e6 __exceptionPreprocess + 294 1 libobjc.A.dylib 0x0000000109e03031 objc_exception_throw + 48 2核心基础0x000000010a7ef784-[NSObject(NSObject)不识别选择器:]+132 3 UIKit 0x000000010ae1973b-[UIResponder doesNotRecognizeSelector:] + 295 4 核心基础 0x000000010a6f0898 ___forwarding___ + 1432 5 核心基础 0x000000010a6f0278 _CF_forwarding_prep_0 + 120 6 UIKit 0x000000010b1e341b-[UIGestureRecognizerTarget_sendActionWithGestureRecognizer:] + 57 7 UIKit 0x000000010b1ec1f0 _UIGestureRecognizerSendTargetActions + 109 8 UIKit 0x000000010b1e9a38 _UIGestureRecognizerSendActions + 307 9 UIKit 0x000000010b1e8c8c-[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 859 10 UIKit 0x000000010b1cdcf0 _UIGestureEnvironmentUpdate + 1329 11 核心基础 0x000000010a710607 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 12 核心基础 0x000000010a71055e __CFRunLoopDoObservers + 430 13 核心基础 0x000000010a6f4b81 __CFRunLoopRun + 1537 14 核心基础 0x000000010a6f430b CFRunLoopRunSpecific + 635 15 图形服务 0x0000000112bb0a73 GSEventRunModal + 62 16 UIKit 0x000000010abeb0b7 UIApplicationMain + 159 17 隆隆声 Ios 0x000000010924b777 主要 + 55 18 libdyld.dylib 0x000000010ebd9955 开始 + 1 ) libc++abi.dylib:以 NSException 类型的未捕获异常终止

CustomTableViewCell.swift中的代码:

import UIKit

class CustomTableViewCell: UITableViewCell {

    @IBOutlet weak var selectButton: UIButton!

    override func awakeFromNib() {
        super.awakeFromNib()

        let longPressGesture = UILongPressGestureRecognizer(target: selectButton, action: #selector(longPress))
        longPressGesture.minimumPressDuration = 1
        self.addGestureRecognizer(longPressGesture)
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

    @objc func longPress() {
        print("longPress")
    }
}

我一直无法通过谷歌搜索到这个问题的答案。我读过的所有内容都表明这些类型的错误是由拼写错误引起的(应该在构建时捕获)。我找不到任何像我遇到的问题一样的问题。

任何帮助将不胜感激!

【问题讨论】:

  • (target: selectButton...=>(target: self... 这些错误与拼写错误无关。只是它试图在UIButtonselectButton)上调用方法longPress(),但它没有它,它是实现它的单元格。

标签: ios swift


【解决方案1】:

您应该将 self 提供给 target 参数并将识别器添加到您的 selectButton

更新代码:

let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress))
longPressGesture.minimumPressDuration = 1
selectButton.addGestureRecognizer(longPressGesture)

【讨论】:

    【解决方案2】:

    变化:

    let longPressGesture = UILongPressGestureRecognizer(target: selectButton, action: #selector(longPress))
    

    到:

    let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress))
    

    目标需要是实现选择器的类。在这种情况下,这是您的单元格,而不是按钮。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-24
      • 1970-01-01
      • 2019-06-20
      相关资源
      最近更新 更多