標籤:blog http io ar os 使用 sp for strong
方法一:
//1, 關閉鍵盤
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
方法二:
//2, 關閉鍵盤
[[self findFirstResponderBeneathView:self] resignFirstResponder];
- (UIView*)findFirstResponderBeneathView:(UIView*)view
{
// Search recursively for first responder
for ( UIView *childView in view.subviews ) {
if ( [childView respondsToSelector:@selector(isFirstResponder)] && [childView isFirstResponder] )
return childView;
UIView *result = [self findFirstResponderBeneathView:childView];
if ( result )
return result;
}
return nil;
}
關閉鍵盤 退出鍵盤 的5種方式
1、點擊編輯區以外的地方(UIView)
2、點擊編輯地區以外的地方(UIControl)
3、使用製作收合鍵盤的按鈕
4、使用判斷輸入字元
5、關於鍵盤遮蔽的問題
1,點擊編輯區以外的地方(UIView)
這是一種很直覺的方法,當不再需要使用虛擬鍵盤時,只要點擊虛擬鍵盤和編輯地區外的地方,就可以將鍵盤收合,下面程式碼是在 UIView 中內建的觸碰事件方法函式,您可以參考 Touch Panel / 觸碰螢幕 / 壓力感應器的基本使用方式一文,找到更多關於觸碰事件的方法函式。
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (![myTextView isExclusiveTouch]) {
[myTextView resignFirstResponder];
}
}
如果要使用此方式請務必記得,你操作畫面的 Custom Class 一定要是 UIView 才行。
畫面的 Custom Class 為 UIView
2. 點擊編輯地區以外的地方(UIControl)
收合虛擬鍵盤的方式與前一種相同,但是如果你的觸碰事件裡已經且寫滿了程式碼,那麼就可以考慮使用,UIControl 的 Touch Up Inside 事件來收合鍵盤,方法是將以下程式碼與 UIControl 的 Touch Up Inside 事件連結即可。
- (IBAction)dismissKeyboard:(id)sender {
[myTextView resignFirstResponder];
}
如果要使用此方式請務必記得,你操作畫面的 Custom Class 一定要是 UIControl 才行。
畫面的 Custom Class 為 UIControl
將收合鍵盤的方法與 UIControl 事件連結
3. 使用製作收合鍵盤的按鈕
當沒有編輯地區以外的地方可供點擊來收合鍵盤,自己製作一個按鈕來收合目前的虛擬鍵盤,也是一個不錯的方法,由於按鈕必須在虛擬鍵盤出現才能顯示於畫面上,因此必須借用 NSNotificationCenter 來協助我們判斷目前鍵盤的狀態。
首先在 viewDidLoad: 事件中,向 NSNotificationCenter 進行註冊,告訴 NSNotificationCenter 我們的 doneButtonshow: 方法函式。
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (doneButtonshow:) name: UIKeyboardDidShowNotification object:nil];
}
現在每當虛擬鍵盤出現時,就會自動呼叫我們自訂的 doneButtonshow: 方法函式,接下來只要在該方法函式裡定義按鈕出現的方法即可。
-(void) doneButtonshow: (NSNotification *)notification {
doneButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
doneButton.frame = CGRectMake(0, 228, 70, 35);
[doneButton setTitle:@"完成編輯" forState: UIControlStateNormal];
[doneButton addTarget: self action:@selector(hideKeyboard) forControlEvents: UIControlEventTouchUpInside];
[self.view addSubview:doneButton];
}
最後是實作按鈕按下去時的 hideKeyboard: 方法函式,務必記得要在函式中移除該按鈕。
-(void) hideKeyboard {
[doneButton removeFromSuperview];
[myTextView resignFirstResponder];
}
4. 使用判斷輸入字元
如果要使用輸入特定字元(例如 return 換行字元)來收合鍵盤,必須先在類別內的 @interface 區段採用 協定,您可以參考 Protocol 協定的使用方式一文,獲得更多關於協定的資訊。
在採用 協定之後,接著實作出協定內的 textView:shouldChangeTextInRange:replacementText:方法函式,此方法函式會在字元輸入時觸發,而回傳的 BOOL 值則代表該字元是否要作用,下列程式碼就是在此方法函式中,使用判斷輸入字元的方式來收合虛擬鍵盤(判斷字元為 return 換行字元)。
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:@"\n"]) {
[myTextView resignFirstResponder];
return NO;
}
return YES;
}
最後別忘記在 viewDidLoad: 事件中,將 UITextView 的代理物件指向自己,這樣程式才能正確找到實作 協定方法函式的類別。
- (void)viewDidLoad {
[super viewDidLoad];
myTextView.delegate = self;
}
5. 關於鍵盤遮蔽的問題
如果您在實作上有遭遇到鍵盤遮蔽編輯地區的問題,可以參考使用 Animation 解決小鍵盤擋住 UITextField 的問題一文,透過 Core Graphic 的 Animation 功能,在鍵盤出現時同時移動編輯地區來解決遮蔽的問題。
方法:
1、手勢(觸背景)關閉鍵盤
-(void)tapBackground //在ViewDidLoad中調用
{
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnce)];//定義一個手勢
[tap setNumberOfTouchesRequired:1];//觸擊次數這裡設為1
[self.view addGestureRecognizer:tap];//添加手勢到View中
}
-(void)tapOnce//手勢方法
{
[self.textFieldName resignFirstResponder];
[self.textFieldEmail resignFirstResponder];
}
2、觸摸背景關閉鍵盤(非手勢)
更改view所指向的對象類,將它的底層類由UIView更改為UIControl。此時,能夠觸發操作方法。
-(IBAction)backgroundTap:(id)sender{
[nameField resignFirstResponder];
[numberField resignFirstResponder];
}
3、Return鍵退出鍵盤,連線Did End On Exit 到這個行為。
- (IBAction)exitKeyboard:(id)sender {
[sender resignFirstResponder];
}
4、如果第一響應者存在於self.view裡面,就可以退出鍵盤
[self.view endEditing:YES];
iOS關閉鍵盤的兩種簡單方法