我是一個在這裏和iOS世界的菜鳥。在我的非常簡單的待辦事項列表iOS應用程序中,我無法解除特定情況下的鍵盤問題。如何解除多個UITextField的鍵盤
我想讓鍵盤在用戶點擊當前文本字段或鍵盤本身之外的任何地方時被忽略。到目前爲止,當用戶點擊UITableView或我的應用上的大多數元素時,我的鍵盤被解散了(謝謝你們這裏的堆棧溢出)。 但是,當用戶點擊另一個UITextField時,鍵盤不會消失,然而,。
僅供參考,以下是我迄今爲止研究的現有線程列表,但尚未解決此問題。 1)How to dismiss keyboard iOS programmatically 2)Resigning First Responder for multiple UITextFields 3)Dismissing the First Responder/Keyboard with multiple Textfields 4)(數至少多,但我記不清:()
這裏就是我所做的迄今:
(in viewDidLoad())
// Add 'tap' gesture to dismiss keyboard when done adding/editing to-do item
var tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapOutside:")
tap.cancelsTouchesInView = true
self.view.addGestureRecognizer(tap)
func tapOutside(tapOutside: UIGestureRecognizer) {
// Dismiss keyboard
self.view.endEditing(true)
}
@IBAction func EditingDidBegin(sender: UITextField) {
// Highlight the text field which user is editing
self.highlightTextField(sender, highlight: true)
}
@IBAction func EditingDidEnd(sender: UITextField) {
// Undo text field highlight
self.highlightTextField(sender, highlight: false)
self.view.endEditing(true) // try this option and not working
self.setEditing(false, animated: true) // try this option and not working
sender.resignFirstResponder() // try this option and not working
UIApplication.sharedApplication().becomeFirstResponder() // try this option and not working
... // below is my code to update the todo item
}
我也試着打印出我視圖的所有subviews.isFirstResponder(),它全部返回false,我也嘗試覆蓋UIViewController的touchesBegan,並且在它裏面調用self.view.endEditing(true)並調用它的super。這也不起作用。
請幫忙。 :(
TIA
UPDATE: 你們是真棒!。DI得到它現在謝謝你們合作,你有幾個錯誤/搞砸了,因爲我正在學習新的框架,所以這裏就是我沒有。
1)我沒有正確設置UITextField委託。 錯誤:我在xcode中按住ctrl-draged textfield並將我的viewController作爲委託鏈接,並認爲應該解決。我仍然需要更好地研究和理解爲什麼。 解決方案:我刪除了ctrl-drag鏈接,並在tableView:cellForRowAtIndexPath中顯式調用myTextField.delegate = self。那就做到了。謝謝@Sidewalker
2)錯誤:我有一個混合的textFieldShouldBeginEditing等和@IBAction func EditingDidBegin。所以我陷入了textFieldShouldBeginEditing接到電話的情況,但EditingDidBegin沒有接到電話。 解決方案:一旦我明確設置了delegate = self,並堅持實現textField ...方法,並且不對textField使用任何@IBAction,事情就會起作用。
嗨Sidekalker, 非常感謝您的回覆。快速端問題:(但與此有關) 1)我原本沒有設置委託,我只實現了UITextFieldDelegate協議。我的方法如textFieldDidBeginEditing被調用時沒有設置委託,因爲您在示例中顯式地顯示了我(myTextField.delegate = self)。這是否與使用Xcode UI鉤住ctrl-drag並選擇我的UIViewController作爲每個UITableViewCell中的UITextField的委託一樣? – wotzup 2015-04-03 21:02:20
2)另一個側欄問題:我現在在我的tableView:cellForRowAtIndexPath中設置myTextField.delegate = self。我沒有看到應用程序行爲的不同。 3)我很困惑爲什麼我的textFieldShouldBeginEditing被調用的項目B,但我的textFieldDidBeginEditing沒有**不被調用。 (上下文示例:我的待辦事項應用程序目前有3個項目:A,B,C。我點擊項目A並獲得對項目A的textFieldShouldBeginEditing和textFieldDidBeginEditing的調用,然後點擊項目B,我只接到調用textFieldSholdBeginEditing,但不調用textFieldDidBeginEditing對於項目A.你能幫助我理解嗎?TIA – wotzup 2015-04-03 21:05:03
1)是的,那些做同樣的事情 – Sidetalker 2015-04-03 21:43:16