Swift 3: 未识别的选择器发送到实例 Xcode 8

12

我已经通过编程创建了一个 UIView,并为其添加了一个 UIPanGestureRecognizer

class ViewController: UIViewController{
    var preludeView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        initViews()
        createConstrants()

        let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: Selector(("handleTap:")))
        preludeView.addGestureRecognizer(panGestureRecognizer)
    }

    func handleTap(recognizer: UIPanGestureRecognizer) {
        print("WORKING!!!!")
    }

    func initViews() {
        ...
    }

    func createConstrants() {
        ...
    }
}

但是,当我点击该视图时,Xcode会抛出一个错误:

2016-07-13 09:24:29.918 Draft_Hypa_02[661:83024] -[Draft_Hypa_02.ViewController handleTap:]: unrecognized selector sent to instance 0x17d94a10 2016-07-13 09:24:29.921 Draft_Hypa_02[661:83024] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Draft_Hypa_02.ViewController handleTap:]: unrecognized selector sent to instance 0x17d94a10' * First throw call stack: (0x249cf91b 0x2416ae17 0x249d52b5 0x249d2ee1 0x248fe238 0x294ae9eb 0x290e984f 0x28f7aff1 0x294afd4f 0x28f3ba57 0x28f38017 0x28f78ec9 0x28f7867b 0x28f49125 0x28f476d3 0x24991dff 0x249919ed 0x2498fd5b 0x248df229 0x248df015 0x25ecfac9 0x28fb1189 0x93144 0x24587873) libc++abi.dylib: terminating with uncaught exception of type NSException

然而,如果我在handleTap函数中删除参数并去掉Selector(("handleTap:"))中的冒号,一切工作正常!

已经花费了一天时间来尝试解决这个问题,非常感激您的帮助!

3个回答

30

如果您使用的是 swift 3,您的 selector 应该像这样:

#selector(self.handleTap(recognizer:))

1
Xcode 报错:"Value of type 'ViewController' has no member 'handleTap'"。 - Raul
3
@Forze,你还需要在方法的第一个参数前面添加下划线。 func handleTap(_ recognizer: UIPanGestureRecognizer) { - Leo Dabus
@Nirav 谢谢,它有效了!你知道在 Swift 3 中我们必须为动作函数设置外部名称吗? - Raul
1
在Swift 3中,要调用第一个参数,必须写出参数名称。您可以查看此链接以获取更多信息。 - Nirav D
@LeoDabus,你的评论帮了我很多! - Luda

6

为了更好地表达此声明,请使用以下代码:

let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(YourViewController.handleTap(_:)))

而不是

let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: Selector(("handleTap:")))

这将使您的代码更清晰,易于理解和维护。请注意,您需要将“YourViewController”替换为您的视图控制器类名。


Xcode 8抛出“ViewController类型的值没有成员'handleTap'”。 - Raul
使用self代替ViewController或你的viewController的名称。 - Mahendra Y
@Forze:handleTap是一个方法的名称,你正在调用func handleTap(sender: UIPanGestureRecognizer){} - Mahendra Y
@Mahendre 我使用了self,但仍然出现相同的错误。 - Markinson
@Markinson,你能发一下你的函数和它的调用语法吗? - Mahendra Y
@Mahendre Y 我搞定了。你的代码是对的。我只是忘记在调用函数之前写我的 .swift 文件的名称了。 - Markinson

2
class ViewController: UIViewController {

    let customView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()

        let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handleTap(_:)))
        customView.addGestureRecognizer(panGestureRecognizer)
    }

    func handleTap(panGesture: UIPanGestureRecognizer) {

    }
}

不要忘记将您的自定义UIView添加到VC中。

我正在initViews()中添加UIView并将此函数放在viewDidLoad()中。 - Raul

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接