Ios 我们可以在UILabel中添加点击手势吗?

Ios 我们可以在UILabel中添加点击手势吗?,ios,swift3,uilabel,uitapgesturerecognizer,Ios,Swift3,Uilabel,Uitapgesturerecognizer,我在UIView中添加了标签并创建了它的插座,然后在UIAPTgestureRecognizer的帮助下添加了功能,但当我点击或单击标签时。标签文本不会更改。我搜索了一个类似的问题,我得到了答案,也是我写的,但标签文本仍然没有改变。用Swift 3.0编写的代码。 这是我写的代码- import UIKit class testingViewController: UIViewController { @IBOutlet weak var testLabel: UILabel!

我在UIView中添加了标签并创建了它的插座,然后在UIAPTgestureRecognizer的帮助下添加了功能,但当我点击或单击标签时。标签文本不会更改。我搜索了一个类似的问题,我得到了答案,也是我写的,但标签文本仍然没有改变。用Swift 3.0编写的代码。 这是我写的代码-

 import UIKit

class testingViewController: UIViewController {

    @IBOutlet weak var testLabel: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
      let tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(showDatePicker))
      tap.numberOfTouchesRequired = 1
      tap.numberOfTapsRequired = 1
      testLabel.addGestureRecognizer(tap)
      testLabel.isUserInteractionEnabled = true// after adding this it worked
        // Do any additional setup after loading the view.
    }

  func showDatePicker(){
    print("testing")
  }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

可以,但需要将标签的
isUserInteractionEnabled
标志设置为true

检查标签的用户交互。见邓肯的答案C@KrunalBhavsar在故事板中启用了userInteraction,但当我添加代码时,它起作用了。请检查是否有其他视图(tranparent视图)覆盖您的标签。如果源代码(项目)不是机密的,您可以共享它吗?不,没有其他视图。@KrunalBhavsar我已经添加了整个类检查它我将添加图像alsouserInteraction已经在情节提要中启用,但是当我添加代码testLabel.isUserInteractionEnabled=true时它工作了,为什么会这样?我不确定。在IB(Interface Builder)中设置标志也应该起作用。我知道我又问了这个问题,但我问了,因为用户交互是从故事板启用的,而不是working@Ronit,我刚刚对它进行了测试,我能够在标签上添加一个点击手势识别器,并通过选中“已启用用户交互”使其做出响应IB中标签上的标志。(事实上,我将手势识别器放在IB中的场景中。我必须编写的唯一自定义代码是在手势识别器上调用的动作。)