2016-09-23 29 views
2

在我們開始使用Swift 3之後,我們一直面臨着這個'無法識別的選擇器發送到實例!錯誤並且無法在合理的時間找到解決方案。所有我們遇到的舊版Swift的答案。這是我們想出的行動聲明Swift 3和無法識別的選擇器發送到實例錯誤的按鈕創建

 testbutton.addTarget(self, action: Selector("testButtonAction:"), for: UIControlEvents.touchDown) 

這是Xcode的「更正」版本,但他們都沒有工作。

 testbutton.addTarget(self, action: Selector(("testButtonAction:")), for: UIControlEvents.touchDown) 

這是動作功能。

 func testButtonAction(sender: UIButton!) { 

      print("Button tapped") 

      } 

回答

7

的斯威夫特3正確的語法是:

testbutton.addTarget(self, action: #selector(testButtonAction(sender:)), for: UIControlEvents.touchDown) 

任何這裏的失誤。現在在編譯時被拾起,而不是運行時間。

3

它應該是這樣的:

testButton.addTarget(self, action: #selector(YourViewController.testButtonAction(sender:)), for: UIControlEvents.touchDown) 
相關問題