zoukankan      html  css  js  c++  java
  • 004-实现点击任意屏幕上非文本框点移除键盘

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

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

    代码如下:

    - (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];
    }
  • 相关阅读:
    shell80set变量
    shell79控制多进程的数量
    shell78管道
    sina sae开发中出现的问题
    html中代码高亮显示
    handlebars模板替换
    打印目录下所有的文件名(包含深层次目录)
    input为disabled提交后得不到该值的解决方法
    Global和Globals
    js算法运算
  • 原文地址:https://www.cnblogs.com/lszwhb/p/3898366.html
Copyright © 2011-2022 走看看