你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS關閉虛擬鍵盤方法匯總

iOS關閉虛擬鍵盤方法匯總

編輯:IOS開發綜合

在iOS應用開發中,有三類視圖對象會打開虛擬鍵盤,進行輸入操作,但如何關閉虛擬鍵盤,卻沒有提供自動化的方法。這個需要我們自己去實現。這三類視圖對象分別是UITextField,UITextView和UISearchBar。 這裡介紹一下UITextField中關閉虛擬鍵盤的幾種方法。

第一種方法,使用它的委托UITextFieldDelegate中的方法textFieldShouldReturn:來關閉虛擬鍵盤。

在UITextField視圖對象如birdNameInput所在的類中實現這個方法。

(BOOL)textFieldShouldReturn:(UITextField *)textField { 
if ((textField == self.birdNameInput) || (textField == self.locationInput)) { 
[textField resignFirstResponder]; 
} 
return YES; 
} 
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if ((textField == self.birdNameInput) || (textField == self.locationInput)) {
[textField resignFirstResponder];
}
return YES;
}

這樣,在輸入框birdNameInput中打開虛擬鍵盤後,輕擊鍵盤的return鍵就會自動關閉掉虛擬鍵盤。

第二種方法,將birdNameInput的屬性中Return Key修改為done,再定義一個方法和Done鍵的Did End On Exit連接。

通過輕擊done鍵觸發這個事件來關閉虛擬鍵盤。

定義的方法如下:

(IBAction) textFieldDoneEditing:(id)sender 
{ 
[sender resignFirstResponder]; 
} 
- (IBAction) textFieldDoneEditing:(id)sender
{
[sender resignFirstResponder];
}

這兩個方法都是輕擊虛擬鍵盤上一個鍵來關閉它。這屬於精確操作,而手指不像鼠標,做這種操作不容易。因此就UI層面而言,這兩個方法都不是最好的方法。 在iphone或ipad屏幕上,虛擬鍵盤占用的面積大小是有限的。通過輕擊虛擬鍵盤之外的區域而關閉虛擬鍵盤。

第三種方法,通過輕擊鍵盤之外的空白區域關閉虛擬鍵盤。

在birdNameInput所屬的視圖控制器類的viewDidLoad方法中定義一個UITapGestureRecognizer的對象,然後將它賦值為它的視圖。

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)]; 
[self.view addGestureRecognizer:tap]; 
[tap release];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
[tap release];

再定義一下選擇器調用的方法dismissKeyboard。

(void)dismissKeyboard { 
[birdNameInput resignFirstResponder]; 
} 
-(void)dismissKeyboard {
[birdNameInput resignFirstResponder];
}

如果屏幕上有多個textField的話,一個一個地列出來就有些麻煩。那麼將方法修改一下,如下:

(void)dismissKeyboard { 
NSArray *subviews = [self.view subviews]; 
for (id objInput in subviews) { 
if ([objInput isKindOfClass:[UITextField class]]) { 
UITextField *theTextField = objInput; 
if ([objInput isFirstResponder]) { 
[theTextField resignFirstResponder]; 
} 
} 
} 
} 
-(void)dismissKeyboard {
NSArray *subviews = [self.view subviews];
for (id objInput in subviews) {
if ([objInput isKindOfClass:[UITextField class]]) {
UITextField *theTextField = objInput;
if ([objInput isFirstResponder]) {
[theTextField resignFirstResponder];
}
}
}
}

如果這個屏幕上的視圖對象很復雜的話,另當別論。 這個方法是編碼新建一個手勢對象。也可以直接使用interface builder圖形化開發工具,在storyboard中拉入一個手勢對象到視圖控制器類中,再將此手勢對象建立一個IBACTION,名稱可以是dismissKeyboard。

第四種方法,通過輕擊鍵盤之外的空白區域關閉虛擬鍵盤。

將屏幕上的view也就是textField的父視圖拖一個touch down事件出來,和一個能關閉虛擬鍵盤的方法連接。如果視圖沒有touch down事件,可將view的父類從UIView修改為UIButton。 首先定義並實現一個方法backgroundTap:。

(IBAction) backgroundTap:(id)sender 
{ 
NSArray *subviews = [self.view subviews]; 
for (id objInput in subviews) { 
if ([objInput isKindOfClass:[UITextField class]]) { 
UITextField *theTextField = objInput; 
if ([objInput isFirstResponder]) { 
[theTextField resignFirstResponder]; 
} 
} 
} 
} 
- (IBAction) backgroundTap:(id)sender
{
NSArray *subviews = [self.view subviews];
for (id objInput in subviews) {
if ([objInput isKindOfClass:[UITextField class]]) {
UITextField *theTextField = objInput;
if ([objInput isFirstResponder]) {
[theTextField resignFirstResponder];
}
}
}
}

然後選擇背景視圖的Touch Down事件,連接 backgroundTap:即可。這樣只要輕擊一下虛擬鍵盤之外的區域,就能關閉虛擬鍵盤。這些方法都是使用resignFirstResponder方法來關閉虛擬鍵盤,還有其他的方法。

第五種方法,使用endEditing:方法 在所在的視圖控制器類中,覆蓋這個方法

(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
[[self view] endEditing:YES]; 
} 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[self view] endEditing:YES];
}

This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to YES, the text field is never even asked; it is forced to resign.

但是,如果這個屏幕很復雜,虛擬鍵盤之外的區域中有很多按鈕。輕擊這些區域時可能會輕擊到這些按鈕,這樣虛擬鍵盤就不能關閉。

要是找到一個沒有按鈕的空白區域都不容易且還有隱藏的視圖對象時,通過輕擊虛擬鍵盤之外的區域關閉虛擬鍵盤的方法實現起來就難了。

第六種方法,覆蓋hitTest:withEvent:方法關閉虛擬鍵盤

在stackoverflow.com上,有人這樣總結。說使用hitTest:withEvent:方法是最好的,也是最容易的解決方法。

I think the easiest (and best) way to do this is to subclass your global view and use hitTest:withEvent method to listen to any touch. Touches on keyboard aren't registered, so hitTest:withEvent is only called when you touch/scroll/swipe/pinch... somewhere else, then call [self endEditing:YES]. This is better than using touchesBegan because touchesBegan are not called if you click on a button on top of the view. It is better than UITapGestureRecognizer which can't recognize a scrolling gesture for example. It is also better than using a dim screen because in a complexe and dynamic user interface, you can't put dim screen every where. Moreover, it doesn't block other actions, you don't need to tap twice to select a button outside (like in the case of a UIPopover). Also, it's better than calling [textField resignFirstResponder], because you may have many text fields on screen, so this works for all of them.

因此,我再建立一個繼承UIView的視圖類。在這個視圖類中,覆蓋hitTest:withEvent:方法,增加[self endEditing:YES]方法。

(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { 
UIView *result = [super hitTest:point withEvent:event]; 
[self endEditing:YES] 
return result; 
} 
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *result = [super hitTest:point withEvent:event];
[self endEditing:YES]
return result;
}

我將視圖控制器的主視圖所屬類修改為這個新建視圖類。這樣在屏幕上輕擊任何位置都會關閉虛擬鍵盤。 這個方法是最簡單,也是最好的關閉虛擬鍵盤的方法。 使用好hitTest:withEvent:這個方法,還可以實現很多很復雜的功能。

The implementation of hitTest:withEvent: in UIResponder does the following:

• It calls pointInside:withEvent: of self
• If the return is NO, hitTest:withEvent: returns nil. the end of the story.
• If the return is YES, it sends hitTest:withEvent: messages to its subviews. it starts from the top-level subview, and continues to other views until a subview returns a non-nil object, or all subviews receive the message.
• If a subview returns a non-nil object in the first time, the first hitTest:withEvent: returns that object. the end of the story.
• If no subview returns a non-nil object, the first hitTest:withEvent: returns self
This process repeats recursively, so normally the leaf view of the view hierarchy is returned eventually. However, you might override hitTest:withEvent to do something differently. In many cases, overriding pointInside:withEvent: is simpler and still provides enough options to tweak event handling in your application.

以上給大家介紹了六種iOS關閉虛擬鍵盤的方法,大家可以根據個人需要選擇一種適合自己比較好的方法。同時也非常感謝大家對本站網站的支持!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved