Trying to Connect an Outlet to a Gesture Recogniser in Storyboard

I connected a tap gesture recogniser to a label in storyboard by dragging a tap gesture recogniser object onto the label. When I connect the outlet to the gesture recogniser I get this error:

How can I connect the tap gesture recogniser in storyboard to my code?

Answered by Claude31 in 847627022

I have created the tap gesture in storyboard and added it to the view controller hierarchy by dragging the tap gesture onto the view controller.

You drag in the viewController, and try to apply to multiple objects (each cell's label: that's what "repeating content" means here). That cannot work as a gesture is attached to a unique object. So you need as many tapGestures as there are cells.

The simplest in your case would be to create the tap gesture in code, in the init of collectionCell.

Or try to declare the tapGesture in storyboard in the cell.

You do not provide enough information about you proceeded.

Did you do those steps ?

  • create a tapGesture in storyboard by dragging the object in the viewController structure ?

-> Could you show the attributes inspector of the tapGesture ?

  • you control dragged in storyboard from the tapGesture to the label object in the viewController structure (in storyboard)?

-> Do you see the tapGesture listed Connections inspector of the label?

You say:

When I connect the outlet to the gesture recogniser I get this error:

How exactly do you try to connect ? Is it the label in storyboard or IBOutlet in code (you should not do it) ? Please clarify

 

How can I connect the tap gesture recogniser in storyboard to my code?

What do you want to connect exactly in code ? You don't have to connect the tapGesture to the label IBOutlet in code. The connection is made in storybaord as explained before.

If you want to declare an action for the tapGesture, then

  • create the IBAction
    @IBAction func tapped(_ sender: UITapGestureRecognizer) {
}
  • select the tapGesture in stroryboard
  • corntrol-drag from the tapgesture to the IBAction

-> You should now see "tapped" in the connections inspector of the tapGesture in the sent actions section.

It seems you have tried to connect the incrementNumberOfTaps action to the cell and not to the tapGesture.

Hi @Claude31 ,

I have created the tap gesture in storyboard and added it to the view controller hierarchy by dragging the tap gesture onto the view controller.

Here is the image of the attributes inspector in storyboard for the tap gesture: I did not control drag the tap gesture to the label in storyboard, rather I dragged the tap gesture and placed it directly on top of the label.

How exactly do you try to connect ? Is it the label in storyboard or IBOutlet in code (you should not do it) ? Please clarify

I control dragged from the tap gesture recogniser to the cell class where I dropped the label, and created the IBAction function called incrementNumberOfTaps when I did this:


protocol TapLabelCollectionViewCellDelegate: AnyObject {
    func didTapLabel()
}

class TapLabelCollectionViewCell: UICollectionViewCell {
    @IBOutlet var taplabel: UILabel!
    
    var delegate: TapLabelCollectionViewCellDelegate?
    
    @IBAction func tapLabelAction(_ sender: Any) {
    }
    
    @IBAction func incrementNumberOfTaps(_ sender: UITapGestureRecognizer) {
    }
}

The circle next to the @IBAction now appears empty.

What do you want to connect exactly in code ? You don't have to connect the tapGesture to the label IBOutlet in code. The connection is made in storybaord as explained before.

I want to connect the gesture recogniser to the @IBAction func incrementNumberOfTaps function so I can call my delegate when I tap the label.

Accepted Answer

I have created the tap gesture in storyboard and added it to the view controller hierarchy by dragging the tap gesture onto the view controller.

You drag in the viewController, and try to apply to multiple objects (each cell's label: that's what "repeating content" means here). That cannot work as a gesture is attached to a unique object. So you need as many tapGestures as there are cells.

The simplest in your case would be to create the tap gesture in code, in the init of collectionCell.

Or try to declare the tapGesture in storyboard in the cell.

Trying to Connect an Outlet to a Gesture Recogniser in Storyboard
 
 
Q