這個問題一直讓我發瘋。看起來無論我嘗試什麼,只要在簡單的Swift程序中顯示,我都無法隱藏鍵盤。不能解僱鍵盤,UITextField委託被稱爲OK
當我在textFieldShouldReturn中resignFirstResponder()時不起作用,在通過從touchesBegan(...)調用endEditing()來處理背景觸摸時它不起作用。我可以看到,當我爲它們設置調試器斷點時,這些相應的方法全部被調用,所以委託被正確設置並按預期調用。
下面是我的具體步驟:
- 創建一個新的單一視圖應用斯威夫特項目
- 將一個的UITextField到視圖,線了IBOutlet中,設置的ViewController了作爲UITextFieldDelegate。
- 在iPhone 6模擬器或iPhone 6設備上運行
- 一旦鍵盤出現,它永遠不會被解僱!
我不知道 - 我錯過了什麼?
這裏是我的ViewController的全部內容:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var myTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
myTextField.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.view.endEditing(true)
}
func textFieldDidBeginEditing(textField: UITextField!) {
return
}
func textFieldShouldEndEditing(textField: UITextField!) -> Bool {
return false
}
func textFieldShouldReturn(textField: UITextField!) -> Bool {
textField.resignFirstResponder()
return true
}
}
哦,對於......我從一些示例代碼中剪切並粘貼了一組UITextDelegate方法,並且完全忽略了其他方法,假設它們是無害的樣板文件。而且你100%正確,當我在Obj-C中進行'等價'測試時,我剛剛實現了我感興趣的回調函數,並且忽略了其他函數,所以它工作正常。咄! – TRB 2014-11-24 17:58:57