【发布时间】:2017-02-27 15:10:22
【问题描述】:
我在 UIView 中添加了标签并创建了它的插座,然后在 UITapGestureRecognizer 的帮助下我添加了功能,但是当我点击或单击标签时。标签文本不会改变。我搜索了类似的问题,我得到了答案,也是我写的,但标签文本仍然没有改变。用 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.
}
*/
}
【问题讨论】:
-
检查标签的用户交互。见邓肯 C 的回答
-
@KrunalBhavsar userInteraction 在情节提要中启用,但是当我添加代码时它起作用了,为什么会这样
-
请检查是否有任何其他视图(透明视图)覆盖您的标签。如果不保密,您可以分享您的源代码(项目)吗?
-
不,没有其他视图。
-
@KrunalBhavsar 我已经添加了整个班级检查它我也会添加图像
标签: ios swift3 uilabel uitapgesturerecognizer