[英]UITextfield's clear button hides keyboard when its inside UIScrollView
[英]Tap on UITextField's clear button hides keyboard instead of clearing text
在iPhone中,我有一個具有UITextField
的視圖。 當我點擊UITextField
的清除按鈕時,鍵盤被解除而不是清除UITextField
的文本。 在iPad上它正常工作。 我該怎么做才能解決這個問題?
只需清除字段resignFirstResponder
(如果要隱藏鍵盤)並返回NO
/ false
注意:設置UITextField
Attributes inspector屬性
清除按鈕 - >編輯時出現
所以它會在文本字段中編輯時顯示清除按鈕。
// Objective-C
-(BOOL)textFieldShouldClear:(UITextField *)textField
{
textField.text = @"";
[textField resignFirstResponder];
return NO;
}
//斯威夫特
func textFieldShouldClear(textField: UITextField) -> Bool {
textField.text = ""
textField.resignFirstResponder()
return false
}
在附加uitextifield的委托后嘗試此代碼
-(BOOL)textFieldShouldClear:(UITextField *)textField
{
return true;
}
首先,檢查與您的UITextField
相關的所有代碼塊(尤其是代碼yourTextField.hidden = YES;
)
放置斷點並分析您實現的每個UITextField
委托。
( textFieldDidEndEditing
, textFieldShouldEndEditing
, textFieldShouldReturn.etc
。 )
要么
實現textFieldShouldClear
委托並在此處編寫代碼以顯示和清除您的UITextField
為此,您必須將clearButtonMode
設置如下,
yourTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
yourTextField.delegate = self;
//For active keyboard again
[yourTextField becomeFirstResponder];
然后實現textFieldShouldClear
委托
YourClass.h
@interface className : UIViewController <UITextFieldDelegate>
YourClass.m
-(BOOL)textFieldShouldClear:(UITextField *)textField {
yourTextField.hidden = NO;
yourTextField.text = @"";
return YES;
}
只要確保你已經給了這兩個
editingTextField.delegate = self;
editingTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
只有在需要進行一些自定義時才需要TextFieldShouldClear :-)
你在做這個方法嗎?
也許你在這個委托方法中調用resignFirstResponder,這就是為什么鍵盤被解雇了。
請仔細檢查委托方法,並檢查您的確切行為。
如果你有這個問題也會發生
yourTextField.clearButtonMode = UITextFieldViewModeNever;
檢查此行並將其刪除或更改查看模式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.