简体   繁体   中英

iOS 9: Gesture Recognizer was setup in a storyboard/xib to be added to more than one view (not working)

Using iOS 9 and facing a problem with a UITapGestureRecognizer . I have a ViewController-A with a UITableView . I have added a tableViewCell which has a textLabel. I want to implement tap on the textLabel. So if I tap on textLabel -- it should print on Console or do anything else

Issue: TapRecogniser is not working. Getting the below error:

在此输入图像描述

Following is what I have done:

1) Added a `UITapGestureRecognizer' on the textLabel (From StoryBoard). Enabled User Interaction for the textLabel (the error even now)

2) Following is the IBAction:

@IBAction func nameTap(sender: UITapGestureRecognizer) {
   print("a")
}

3) CellForRowAtIndexPath

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! ThirdViewCell!
    cell.nameLabel?.text = "XYZ"

    let nameTapRecognizer = UITapGestureRecognizer(target: self, action: Selector("nameTap:"))
    nameTapRecognizer.cancelsTouchesInView = false
    cell.nameLabel?.addGestureRecognizer(nameTapRecognizer)
    return cell
}

PS: 1) This was working in iOS 8. I have checked..There are no duplicates (there is only one tap recognizer in the entire file and its linked to the textLabel)

2) I don't want to use didSelectRowAtIndexPath method as I need to implement TapGestureRecognizer for more textLabels within the tableViewCell.

are you see the error console Label , and the property as UserInteractionEnabled = NO; see the screen shot

在此输入图像描述

try this

let nameTapRecognizer = UITapGestureRecognizer(target: self, action: Selector("nameTap:"))
nameTapRecognizer.cancelsTouchesInView = false
cell.nameLabel?.tag = indexPath.row // add this 
nameTapRecognizer.numberOfTapsRequired = 1 // add this 
nameTapRecognizer.delegate =self
cell.nameLabel?.userInteractionEnabled = true  // add this 
cell.nameLabel?.addGestureRecognizer(nameTapRecognizer)

// method
func nameTap(gesture: UITapGestureRecognizer) {
let indexPath = NSIndexPath(forRow: gesture.view!.tag, inSection: 0)
let cell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell

// Do whatever you want.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM