2011-01-29 120 views
0

我在視圖中有四個textField。 前三個用標準鍵盤編輯。 最後一個編輯器使用一個選擇器作爲textField的inputView屬性。 這很酷,因爲它處理幻燈片上下。如何向下滑動一個鍵盤向上滑動自定義一個

想象一下,我正在用標準鍵盤在前三個textField中的一箇中輸入內容。 現在我點擊第四行。 我希望標準鍵盤縮回並使拾取器向上滑動。 現在它只是即時交換。

我試過使用textField委託方法在標準鍵盤上寫入resignFirstResponder的邏輯,併成爲使用選取器的第一響應者,但它仍然立即改變。這是代碼。

- (void)textFieldDidBeginEditing:(UITextField *)textField{ 
//If we go from a keyboard field to a picker field 
if ((self.textFieldBeingEdited == self.nameField || self.locationField || self.controllerSNField)){ 
    if (textField == equipTypeField) { 

     //Put away the keyboard 
     [self.textFieldBeingEdited resignFirstResponder]; 

     //Show the Picker 
     [textField becomeFirstResponder]; 
    } 
} 
} 

我還需要編寫邏輯來選擇器滑下向上滑動鍵盤,如果我從第四視圖任何其他人去。但如果我能得到上半場,第二場應該很明顯。

任何想法如何做到這一點,而不進入動畫塊?也許與通知?

謝謝。

回答

1

問題可能是您已經開始編輯該方法的時間,因此resignFirstResponder的調用不起作用,因爲它不再是第一響應者。也許如果你再次成爲第一響應者,它可能會奏效。但在equipTypeField上調用becomeFirstResponder可能會導致textFieldDidBeginEditing再次被調用,從而導致您陷入無限循環。毫無疑問,窘境。但最起碼,我會嘗試這樣的:

//So that the keyboard that animates out is the one that was previously showing 
[self.textFieldBeingEdited becomeFirstResponder]; 
//Animate the keyboard out 
[self.textFieldBeingEdited resignFirstResponder]; 
//Don't let the user screw anything up 
[[UIApplication sharedApplication] beginIgnoringInteractionEvents]; 
//Animate the new keyboard in a little while later 
[textField performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.25]; 
//Let the user interact with the application again after the animation completes 
[[UIApplication sharedApplication] performSelector:@selector(endIgnoringInteractionEvents) withObject:nil afterDelay:0.25]; 

這仍然留下了哪裏把這段代碼的問題,因爲如果你把它放在textFieldDidBeginEditing,你可能會得到一個無限循環。我的建議是嘗試一下,看看會發生什麼。如果它工作,那麼很好,否則,放入某種布爾標誌或其他東西以確保在調用didFinishEditing之前只調用它一次。

+0

它沒有工作,但我從你的解決方案中學到了一兩件事。我明白你對無限循環的含義。謝謝。 – Aaronium112 2011-01-29 14:35:26