我在UIView的增加標籤和造就了其出口,然後用我所添加的功能UITapGestureRecognizer的幫助,但是當我點擊或單擊標籤。標籤文字不會更改。我搜索了類似的問題,我的答案也是我寫的,但標籤文本仍然沒有改變。代碼用Swift 3.0編寫。 這是我寫的代碼 -我們可以將Tap Gesture添加到UILabel嗎?
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.
}
*/
}
檢查用戶交互。看到鄧肯ç – Krunal
@KrunalBhavsar用戶交互的答案情節板中的啓用,但是當我添加的代碼,然後它的工作爲什麼這麼 – Ronit
請檢查,如果有超過拍打你的標籤的任何其他視圖(tranparent視圖)。如果它不是機密的,你可以分享你的源代碼(項目)嗎? – Krunal