我有兩個文本字段。點擊後,第一個顯示一個QWERTY鍵盤,第二個顯示選擇器。 我的問題是當我點擊我的第一個textField時,鍵盤正常顯示。現在,在我輸入任何東西之後,我直接想要點擊第二個textField,這是顯示選擇器,鍵盤應該消失,但鍵盤仍然存在。 我想單擊第二個textField時立即隱藏鍵盤。鍵盤不隱藏時點擊第二個文本字段
回答
有兩種方式來實現這一點 -
第一:使用UITextFieldDelegate。 對於覺得─ 列出UITextFieldDelegate在您的視圖控制器的協議列表一樣,
@interface ViewController : UIViewController <UITextFieldDelegate>
然後讓你的ViewController符合文本字段的委託,以實現像textFieldDidBeginEditing
的方法,並textFieldDidEndEditing:
所以,去到viewDidLoad
方法和符合的UITextField的協議喜歡 -
- (void)viewDidLoad
{
[super viewDidLoad];
self.firstTextField.delegate = self; //assuming your first textfield's
//name is firstTextField
//Also give your first textfield a tag to identify later
self.firstTextField.tag = 1;
}
現在,你都設置爲實現委託方法。但是,要首先實現目標,需要使用UITextField實例來了解何時輸入firstTextField。因此,聲明UITextField
類型的屬性。這樣做,在接口文件喜歡 -
@property(nonatomic, strong) UITextField *currentTextField;
現在在textFieldDidBeginEditing
委託方法中,firstTextField實例分配到currentTextField當您開始鍵入它喜歡 -
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if(textField.tag == 1){
self.currentTextField = textField;
}
}
後,在textFieldDidEndEditing
方法,檢查它是否是當前的文本框要從中走出來,並取消鍵盤喜歡 -
-(void)textFieldDidEndEditing:(UITextField *)textField{
if(textField.tag == 1){
[self.currentTextField resignFirstResponder];
}
return YES;
}
秒:您可以使用UIResponder。作爲ViewControllers從UIResponder繼承,你只需重寫方法 - touchesBegan:withEvent
方法,一些喜歡 -
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];// this will do the trick
}
在這種情況下,當你點擊了側的文本字段,鍵盤會自動消失。
使用UITextFieldDelegate
組代表在ViewDidLoad
self
,然後把這個代碼
func textFieldDidEndEditing(textField: UITextField)
{
yourtextfieldname.resignFirstResponder()
return true
}
我正在這樣做,但它不工作 –
Step 1 :- Code for ViewController.h :-
//add UITextFieldDelegate just after UIViewController
@interface ViewController : UIViewController<UITextFieldDelegate>
Step 2 :- Code For ViewController.m :-
//inside viewdidLoad write following code, where txtInput is outlet for input text field
_txtInput.delegate = self;
// Last Thing write following code just above @end
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
//that's it use this very simple way
- 1. 的XCode文本字段隱藏鍵盤
- 2. 點擊UIWebView隱藏鍵盤
- 3. 隱藏第二次點擊
- 4. 隱藏鍵盤時點擊Ext.Form.Select
- 5. 虛擬鍵盤隱藏字段/文本域/ CONTENTEDITABLE(下面隱藏鍵盤)
- 6. 點擊一個UIScrollView隱藏鍵盤?
- 7. 在點擊編輯文本字段第二次顯示的默認鍵盤
- 8. 如何知道鍵盤何時隱藏文本字段?
- 9. 測試鍵盤在顯示時是否隱藏文本字段
- 10. 當按鈕點擊時隱藏鍵盤(片段)
- 11. 爲什麼文本字段不能響應隱藏鍵盤?
- 12. 移動視圖,以便鍵盤不隱藏文本字段
- 13. 鍵盤隱藏文本框
- 14. UIWebView在第二次加載時關注文本字段時不顯示鍵盤
- 15. Android中WebView中的文本字段被鍵盤隱藏
- 16. Android隱藏鍵盤密碼字段
- 17. iPhone鍵盤在UIWebView中隱藏字段
- 18. 隱藏鍵盤密碼字段Android
- 19. 鍵盤隱藏輸入字段的WebView
- 20. 隱藏文本被點擊
- 21. UITextField失去焦點時隱藏鍵盤
- 22. 帶文本字段的UIAlertController - 點擊返回按鈕只隱藏鍵盤,不執行操作?
- 23. 如何使用背景隱藏數字鍵盤點擊?
- 24. 只有在ipad上點擊某個文本字段時才顯示鍵盤?
- 25. 點擊ListView中的複選框時隱藏鍵盤
- 26. iPhone:點擊UITextField時是否可以隱藏鍵盤?
- 27. 如何在editText外部點擊時隱藏鍵盤?
- 28. 當完成點擊時,軟鍵盤未隱藏AutocompleteTextView
- 29. 隱藏鍵盤按鈕被點擊時xamarin形式
- 30. 隱藏鍵盤
第一點擊後第二文本字段被做self.view.endEditing則目前選擇器 –
我沒有...但不工作 –
..刪除textfild&使用按鈕爲什麼使用textfield –