[英]ResignFirstResponder not working
-(IBAction)settings:(id)sender
{
[mileagerate resignFirstResponder];
[mainView bringSubviewToFront:mileageView];
mainView.hidden=TRUE;
[UIView beginAnimations:@"Settings" context:nil];
[UIView setAnimationDuration:1.0];
[mainView setFrame:CGRectMake(0, 0, 320, 460)];
[UIView commitAnimations];
mileagerate.text = [[NSUserDefaults standardUserDefaults] valueForKey:@"savedstring"];
mileageView.hidden=FALSE;
}
我在我的應用程序的各個地方使用了辭職第一響應者。 但是當我單獨點擊這個按鈕時,鍵盤並沒有消失。 如果我點擊鍵盤上的返回鍵,輸入的值就會消失。
如果您不知道要辭職的textfield
,請在您的方法中使用以下代碼行:
self.view.endEditing(true)//resign current keyboard
[self.view endEditing:YES]; //resign current keyboard
編輯:注意:它將從您當前的view
中resign keybord
。
如果您已將TextFieldDelegate放在那里,請檢查您的 .h 文件。 . 有時如果我們不聲明它的委托函數就不能正常工作。
我認為當你設置文本時, textField mileagerate 再次成為第一響應者,所以在方法的最后使用這個語句
[mileagerate resignFirstResponder];
您必須首先使用[UIView setAnimationDidStopSelector:@selector(yourMethod)];
然后在“ YourMethod
”中退出你的鍵盤。
- (void)yourMethod
{
[mileagerate resignFirstResponder];
}
在 Swift 2 中,您可以使用以下 3 種不同的類型來關閉鍵盤(假設有一個文本字段和一個按鈕
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet var userInputField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
//Important if you are using textfieldDelegate
self.userInputField.delegate = self
}
@IBAction func findAgeBtn(sender: UIButton) {
//1. keyboard to disappear when button clicked
dimissKeyboard()
}
//2. keyboard to disappear when touched anywhere on the view
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
//Function for option 1
func dimissKeyboard(){
self.view.endEditing(true)
}
//3. keyboard to disappear when clicked on the Return key. Don't forget to set the delegate to self
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
確保您已連接返回鍵以退出第一個響應者
在連接選項卡上將連接更改為“退出時結束”並連接到視圖控制器(橙色球),將鍵盤返回鍵更改為完成。
嘗試這個:
if([mileagerate isFirstResponder])
[mileagerate resignFirstResponder];
1.檢查 UITextFieldDelegate ,
class YourClass : UIViewController,UITextFieldDelegate{
2.然后覆蓋功能“應該結束編輯”返回到是
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
return true
}
斯威夫特 4+
我將self.view.endEditing(true)
放在DispatchQueue中,它對我有用。
DispatchQueue.main.async { self.view.endEditing(true) }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.