Swift:“无法识别的选择器已发送到实例”尝试使用轻击手势时出现错误 [英] Swift: "Unrecognized selector sent to instance" error when trying to use tap gesture

查看:60
本文介绍了Swift:“无法识别的选择器已发送到实例”尝试使用轻击手势时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的错误


***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[App .DetailController tap]:无法识别的选择器发送到实例0x109803800'

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[App.DetailController tap]: unrecognized selector sent to instance 0x109803800'

我的名为 DetailController的视图控制器的imageView很小,当用户单击图像,我希望图像放大到全屏,然后再次单击以返回全屏之前的默认图像尺寸。

My view controller called 'DetailController' has a small imageView and when the user clicks the image, I want the image to enlarge to full screen, then when clicked again to return to the default image size it had before the full screen.

问题是当单击imageView时,我的应用程序崩溃了。

Problem is that my app is crashing when the imageView is being clicked.

ViewDidLoad

ViewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()

    iconImage.isUserInteractionEnabled = true
    let tapGesture = UITapGestureRecognizer(target: self, action: Selector(("tap")))
    iconImage.addGestureRecognizer(tapGesture)
}

func tap() {

    let screenSize: CGRect = UIScreen.main.bounds
    let screenWidth = screenSize.width
    let screenHeight = screenSize.height
    iconImage.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight)
}


推荐答案

请勿使用 Selector()。使用 #selector()表单。

Don't use Selector(). Use the #selector() form. The compiler is able to check for a matching method with that form.

对于姿势识别器,选择器应具有1个参数:姿势识别器本身:

And for a gesture recognizer, the selector should have 1 parameter: The gesture recognizer itself:

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tap(_:)))

您的函数将如下所示

@IBAction func tap(_ gesutureRecognizer: UITapGestureRecognizer) {
}

对于一个函数 UIViewController 的函数,您不需要使用 @objc 限定符,因为 UIViewController 是一个Objective-C对象。

For a function of a UIViewController you shouldn't need the @objc qualifier on the function, since a UIViewController is an Objective-C object.

这篇关于Swift:“无法识别的选择器已发送到实例”尝试使用轻击手势时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆