2015-04-03 86 views
1

我是一個在這裏和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,事情就會起作用。

回答

0

這裏有一個選項...我們要添加一個布爾標誌,以確定在文本框無論我們是否是在爲另一個文本字段的編輯試圖開始

讓你的類堅持UITextFieldDelegate

class MyClass: UIViewController, UITextFieldDelegate 

不要忘了設置委託,我們將添加標誌以及

myTextField.delegate = self 
var inField = false 

貫徹 「textFieldShouldBeginEditing」 和 「textFieldDidBeginEditing」

func textFieldShouldBeginEditing(textField: UITextField) -> Bool { 
    if inField { 
     inField = false 
     return false 
    } 
    return true 
} 

func textFieldDidBeginEditing(textField: UITextField) { 
    inField = true 
} 

我更喜歡跟蹤這樣的事情,而不是識別子視圖,因爲它允許在其他地方使用該標誌並降低代碼複雜度。

+0

嗨Sidekalker, 非常感謝您的回覆。快速端問題:(但與此有關) 1)我原本沒有設置委託,我只實現了UITextFieldDelegate協議。我的方法如textFieldDidBeginEditing被調用時沒有設置委託,因爲您在示例中顯式地顯示了我(myTextField.delegate = self)。這是否與使用Xcode UI鉤住ctrl-drag並選擇我的UIViewController作爲每個UITableViewCell中的UITextField的委託一樣? – wotzup 2015-04-03 21:02:20

+0

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

+0

1)是的,那些做同樣的事情 – Sidetalker 2015-04-03 21:43:16

0

那麼鍵盤不會消失,因爲它並不期望必須。新的UITextField正在成爲第一個響應者,而另一個辭職。如果您不希望textField成爲第一個響應者(如果已經有另一個響應者),那麼您必須在獲得機會之前將其切斷。我會嘗試實現textFieldShouldBeginEditing並找出那裏的邏輯。

我不喜歡這種看起來的樣子,但這應該沿着這條線做點什麼。

func textFieldShouldBeginEditing(textField: UITextField) -> Bool { 
    for subView in self.view.subviews{ 
     if(subView.isKindOfClass(UITextField)){ 
      if(subView.isFirstResponder()){ 
       subView.resignFirstResponder(); 
       return false; 
      } 
     } 
    } 
    return true; 
} 
+0

嗨user3117251, 非常感謝您的回覆和示例解決方案。我嘗試過,但它仍然沒有工作:(但是,它給了我一些好主意,嘗試實現textFieldShouldBeginEditing來弄清楚事情進一步,但謝謝! 我試過你的代碼,並且如果條件(subView.isFirstResponder() – wotzup 2015-04-03 20:39:51

+0

一個有趣的發現是: (上下文:todo應用程序有3個樣品todo項目A,B,C) 1)當我第一次啓動應用程序,並點擊項目A - UITextField。到textFieldShouldBeginEditing和textFieldDidBeginEditing。**然後**,然後我點擊項目B,我只接到調用項目B的textFieldShouldBeginEditing,項目A的textFieldDidEditingEnd(按此確切順序),但**從不**調用了textFieldDidEditingBegin項目B – wotzup 2015-04-03 20:40:58

+0

啊,如果didEndEditing首先被調用,這會很有意義。嘗試從didEnd中刪除sender.resignFirstResponder()編輯方法。只要先得到要求,你就說對了,這種情況永遠不會是真的。我已經完成了它,並在我做的示例應用程序中工作,除非其他事情正在進行,否則應該假設工作。 – Dare 2015-04-03 20:44:40

0

首先將所有的UITextField(你正在創建)delegate設置爲self並創建一個UITextField成員變量。現在實現「textFieldDidBeginEditing」委託方法並將文本字段分配給您的成員UITextField變量。至於那麼現在,每當你想取消鍵盤呼籲「yourMemberVariable」對象的解僱方法如下

func textFieldDidBeginEditing(textField: UITextField) { 
    yourMemberVariable = textField; 
} 

給出。它應該工作!

0

我最常做的是實施這種兩種方法:

第一個加UITapGestureRecognizer整個的UIViewController視圖

func hideKeyboard() { 
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard)) 
    view.addGestureRecognizer(tap) 
} 

第二個剛剛被調用,每次在用戶觸摸任何地方UIViewController的視圖

func dismissKeyboard() { 
    self.view.resignFirstResponder() 
} 

我將第一個添加到UIViewController的viewDidLoad方法。或者更好的是,如果你想在所有的應用程序中使用它,只需將它作爲你的UIViewController的擴展。