UITapGestureRecognizer Only Works on Last Item

Viewed 903

I'm creating a resume app (mostly for fun) and can't seem to get the tap gesture recognizer to work for me. What I'd like is to have a single label display the information and have it change depending on which title label they tap below. Here is the code I've written:

import UIKit

class WorkHistoryViewController: UIViewController {

    // MARK: Properties

    @IBOutlet weak var jobOne: UILabel!
    @IBOutlet weak var jobTwo: UILabel!
    @IBOutlet weak var jobThree: UILabel!
    @IBOutlet weak var jobFour: UILabel!
    @IBOutlet weak var jobFive: UILabel!
    @IBOutlet weak var workHistoryDescriptionLabel: UILabel!
    let tapRec = UITapGestureRecognizer()

    override func viewDidLoad() {
        super.viewDidLoad()
        tapRec.addTarget(self, action: "tappedLabel")
        jobOne.addGestureRecognizer(tapRec)
        jobTwo.addGestureRecognizer(tapRec)
        jobThree.addGestureRecognizer(tapRec)
        jobFour.addGestureRecognizer(tapRec)
        jobFive.addGestureRecognizer(tapRec)
    }

    // MARK: Methods

    func tappedLabel() {
        workHistoryDescriptionLabel.text = "It worked!"
    }
}

What's happening is that the last label to have addGestureRecognizer() called in viewDidLoad() is the only one that works. If I comment out the last line then only the label above it works. I also tried to enable user interaction on each of the labels programmatically and on the attributes inspector and neither changed anything.

4 Answers
Related