Swift:尝试使用点击手势时出现"Unrecognized selector sent to instance"错误



我得到的错误

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

我的视图控制器称为"详细信息controller"的图像视图很小,当用户单击图像时,我希望图像放大到全屏,然后再次单击以返回到全屏幕之前的默认图像大小。

问题是我的应用在单击imageView时正在崩溃。

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()表格。编译器能够检查使用该表格的匹配方法。

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

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

,您的功能看起来像这样

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

对于UIViewController的函数,您不需要该功能上的@objc预选赛,因为UIViewController是Objective-C对象。

相关内容

最新更新