Can't drag IBAction from UITapGestureRecognizer in Storyboard

时间:2016-02-12 21:24:43

标签: ios xcode swift ibaction xcode-storyboard

I am trying to drag an IBAction from my UITapGestureRecognizer, which was placed on an UIImageView that resides in a custom tableview cell.

However, it does not allow me to drag an action from storyboard to the custom tableview cell's subclass

The cell's class is properly set to the correct file in the identity inspector. And user interaction is enabled on the imageview.

So I tried typing out the IBAction and connecting it from the file to storyboard, and it gives this error:

Error after connecting IBAction

This is perplexing because I am able to drag IBActions from UIButtons and other view objects; just not this UITapGestureRecognizer. I have recently updated Xcode to 7.2.1

Here's my view hierarchy from the document outline:

Custom cell document outline

Here's the tap gesture's connections:

imageview's connections

And the imageview's connections:

enter image description here

2 个答案:

答案 0 :(得分:3)

我总是在代码中添加tap手势识别器对象而不是在Storyboard上工作,现在许多Xcode版本发生了变化(目前8.1,swift 3.0)。在故事板上尝试使用Tap Gesture Regnizer时,我重新审视了一些类似的问题。即使我正在使用添加了Tap Gesture Recognizer对象的简单UIImageView(拖放Tap Gesture Recognizer对象到您的图像视图中),我也登陆了这个页面。在这里,我列出了一个小步骤教程,并希望它对像我这样最终出现在这个页面上的其他人有用。

1)在继续

之前,确保您的图像视图在检查器中具有这些连接

enter image description here

2)在快速代码中添加了@IBAction响应函数。

 // MARK: Actions
  @IBAction func tapGestureAction (_ sender: UITapGestureRecognizer){
    print("Tap on Flower Image View")
  }

3)在Main.storyboard上,单击“文档大纲”或视图控制器场景中的图标集合中的“点击手势”对象。
enter image description here

enter image description here

4)转到Connections Inspector并按住Ctrl键并单击“已发送操作”中“选择器”旁边的加号,然后将其拖动到视图的控制器图标。释放鼠标后,您将看到步骤2)中定义的功能名称。您可以选择并且应该建立连接。

enter image description here

enter image description here

5)如果连接成功,Tap Gesture Recognizer对象的Connections Inspector应如下所示

enter image description here

答案 1 :(得分:1)

正如beyowulf在上面的评论中指出的那样,iOS 9中的一个变化改变了UIGestureRecognizers如何处理重复内容。

这很不幸,因为我发现最好使用故事板而不是程序化实现,但是如果你想在这里找到这个代码:

在自定义单元格类中:

override func awakeFromNib() {
    super.awakeFromNib()

    let imageViewDoubleTap = UITapGestureRecognizer(
         target: self,
         action:#selector(ThisClass.userSwiped)
         )
    imageViewDoubleTap.numberOfTapsRequired = 2
    self.photoImageView.addGestureRecognizer(imageViewDoubleTap)
}

然后创建一个与您提供action参数同名的func,并在那里实现您的代码。