码迷,mamicode.com
首页 > 其他好文 > 详细

004-实现点击任意屏幕上非文本框点移除键盘

时间:2014-08-08 01:47:15      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   for   ar   cti   div   

实现点击任意屏幕上非文本框点移除键盘

  我们在开发的过程中若要实现此功能只要将下面这些代码拷贝到你要实现的项目里面就可以实现此功能.

代码如下:

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setUpForDismissKeyboard];
}
- (void)setUpForDismissKeyboard {
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    UITapGestureRecognizer *singleTapGR =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(tapAnywhereToDismissKeyboard:)];
    NSOperationQueue *mainQuene =[NSOperationQueue mainQueue];
    [nc addObserverForName:UIKeyboardWillShowNotification
                    object:nil
                     queue:mainQuene
                usingBlock:^(NSNotification *note){
                    [self.view addGestureRecognizer:singleTapGR];
                }];
    [nc addObserverForName:UIKeyboardWillHideNotification
                    object:nil
                     queue:mainQuene
                usingBlock:^(NSNotification *note){
                    [self.view removeGestureRecognizer:singleTapGR];
                }];
}

- (void)tapAnywhereToDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer {
    //此method会将self.view里所有的subview的first responder都resign掉
    [self.view endEditing:YES];
}

 

004-实现点击任意屏幕上非文本框点移除键盘,布布扣,bubuko.com

004-实现点击任意屏幕上非文本框点移除键盘

标签:style   blog   color   io   for   ar   cti   div   

原文地址:http://www.cnblogs.com/lszwhb/p/3898366.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!