2011-08-02 149 views

回答

60
[myTextField resignFirstResponder] 

Here,管理鍵盤部分的第二段。

6

我建議你添加和行動上你的頭文件:

-(IBAction)removeKeyboard; 

並在實施,寫這樣的事:

-(IBAction)removeKeyboard 
{ 
[self.textfield resignFirstResponder]; 
} 

在NIB文件,從UITextFiled連接文件的所有者在選項DidEndOnExit。這樣,當你按回車時,鍵盤將消失。

希望它有幫助!

183

如果您有多個文本字段並且不知道哪一個是第一響應者(或者您根本無法訪問您編寫此代碼的任何地方的文本字段),您可以在父視圖上調用endEditing:文本字段。

在視圖控制器的方法,它是這樣的:

[self.view endEditing:YES]; 

參數強制文本字段辭職第一響應狀態。如果您使用的是委託執行驗證,並希望停止一切直到文本字段的內容是有效的,你也可以這樣的代碼是:

BOOL didEndEditing = [self.view endEditing:NO]; 
if (didEndEditing) { 
    // on to the next thing... 
} else { 
    // text field must have said to first responder status: "never wanna give you up, never wanna let you down" 
} 

endEditing:方法是不是告訴單獨的文本字段resignFirstResponder好得多,但由於某種原因,直到最近我才發現它。

+70

該死!我不敢相信我剛剛在代碼評論中被rickrolled。 – Lytol

1

您只需將yourTextFieldName替換爲,您猜對了!你的文本框。這將關閉鍵盤。

[yourTextFieldName resignFirstResponder]; 
1
-(void)methodName 
{ 
    [textFieldName resignFirstResponder]; 
} 

調用此方法(方式)與didEndOnExit

2

如果你不知道哪個文本框是你能找到它的第一個響應者。我用這個功能:

UIView *resignFirstResponder(UIView *theView) 
{ 
    if([theView isFirstResponder]) 
    { 
     [theView resignFirstResponder]; 
     return theView; 
    } 
    for(UIView *subview in theView.subviews) 
    { 
     UIView *result = resignFirstResponder(subview); 
     if(result) return result; 
    } 
    return nil; 
} 

然後在您的代碼調用:

UIView *resigned = resignFirstResponder([UIScreen mainScreen]); 
6

在視圖控制器YourViewController.h文件,確保您實現UITextFieldDelegate協議:

@interface YourViewController : <UITextFieldDelegate> 

@end 

然後,在YourViewController.m文件中,執行以下實例方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    [self.yourTextField1 resignFirstResponder]; 
    [self.yourTextField2 resignFirstResponder]; 
    ... 
    [self.yourTextFieldn resignFirstResponder]; 
} 
8

有些情況下,沒有文本字段是第一響應者,但鍵盤在屏幕上。 在這些情況下,上述方法無法關閉鍵盤。

一個例子,如何到達那裏:

  1. 推ABPersonViewController屏幕上的程序;打開任何聯繫人;
  2. 觸摸「筆記」字段(成爲第一響應者並觸發鍵盤);
  3. 在任何其他字段上向左滑動即可顯示「刪除」按鈕;
  4. 到此爲止,您在文本字段中沒有第一響應者(只是以編程方式檢查),但鍵盤仍然存在。調用[view endEditing:YES]什麼也不做。

在這種情況下,你還需要問視圖控制器退出編輯模式:

[viewController setEditing:NO animated:YES]; 
4

我發現那裏endEditingresignFirstResponder失敗的情況。這在那些情況下適用於我。

[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];  
[self setEditing:NO]; 
2

這將辭職一個特定的文本字段

// Swift 
TextField.resignFirstResponder() 

// Objective C 
[TextField resignFirstResponder]; 

辭職下面的代碼

// Swift 
self.view!.endEditing(true) 

// Objective C 
[self.view endEditing:YES]; 
0
-(void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    // your code 

    [textField reloadInputViews]; 
} 
-1
  1. 任何文本字段中設置了「真的結束退出時「Xcode中的事件(右鍵單擊您的文本字段)。

    Screenshot

  2. 實現此方法:

    -(IBAction) closeKeyboard:(id) sender { 
        [_txtField resignFirstResponder]; 
    } 
    
1

對於斯威夫特3

您可以隱藏鍵盤這樣的:

textField.resignFirstResponder() 

如果要隱藏鍵盤,當用戶按下「前奏」按鈕,你必須實現以下UITextFieldDelegate方法:

func textFieldShouldReturn(_ textField: UITextField) -> Bool { 

    textField.resignFirstResponder() 

    return true 
}