简体   繁体   English

iOS 9:Gesture Recognizer在storyboard / xib中设置,可添加到多个视图中(不工作)

[英]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 . 使用iOS 9并面临UITapGestureRecognizer的问题。 I have a ViewController-A with a UITableView . 我有一个带有UITableView的ViewController-A。 I have added a tableViewCell which has a textLabel. 我添加了一个tableViewCell,它有一个textLabel。 I want to implement tap on the textLabel. 我想在textLabel上实现点击。 So if I tap on textLabel -- it should print on Console or do anything else 因此,如果我点击textLabel - 它应该在控制台上打印或执行其他任何操作

Issue: TapRecogniser is not working. 问题: TapRecogniser无效。 Getting the below error: 得到以下错误:

在此输入图像描述

Following is what I have done: 以下是我所做的:

1) Added a `UITapGestureRecognizer' on the textLabel (From StoryBoard). 1)在textLabel(From StoryBoard)上添加了一个“UITapGestureRecognizer”。 Enabled User Interaction for the textLabel (the error even now) 为textLabel启用了用户交互(即使是现在的错误)

2) Following is the IBAction: 2)以下是IBAction:

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

3) CellForRowAtIndexPath 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) PS: 1)这在iOS 8中工作。我检查过..没有重复项(整个文件中只有一个点击识别器,并且它链接到textLabel)

2) I don't want to use didSelectRowAtIndexPath method as I need to implement TapGestureRecognizer for more textLabels within the tableViewCell. 2)我不想使用didSelectRowAtIndexPath方法,因为我需要为tableViewCell中的更多textLabel实现TapGestureRecognizer。

are you see the error console Label , and the property as UserInteractionEnabled = NO; 你看到错误控制台Label ,并且属性为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.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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