2017-02-27 82 views
-2

我在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. 
    } 
    */ 

} 
+1

檢查用戶交互。看到鄧肯ç – Krunal

+0

@KrunalBhavsar用戶交互的答案情節板中的啓用,但是當我添加的代碼,然後它的工作爲什麼這麼 – Ronit

+0

請檢查,如果有超過拍打你的標籤的任何其他視圖(tranparent視圖)。如果它不是機密的,你可以分享你的源代碼(項目)嗎? – Krunal

回答

6

當然可以,但你需要標籤的isUserInteractionEnabled標誌設置爲標籤真正

+0

用戶交互是在故事板已經啓用,但是當我添加的代碼testLabel.isUserInteractionEnabled =真正的工作,爲什麼會這樣呢? – Ronit

+1

我不確定。在IB(Interface Builder)中設置標誌也應該可以工作。 –

+0

我知道我又提出這樣的問題,但我問,因爲用戶交互從故事板啓用,它不工作 – Ronit

相關問題