UITextField点击textField外任意区域键盘回收(一)---- [UIAppllication shareApplication]

来源:互联网 发布:博爱网络智能路由中继 编辑:程序博客网 时间:2024/05/21 08:02

最近做的项目用到好多textField, 涉及到键盘回收,键盘挡住textField 上移textField等,自己水平不够,深感textfield好复杂,想好好整理下自己会得用法,继续学习不会的用法。。

键盘回收有好多中方法:UITextFieldDelegate, 在self.view上添加UIControl, TouchesBegan方法,[UIAppllication shareApplication]方法

其中点击textField外任意区域回收键盘,这三种方法可以实现:self.view上添加UIControl, TouchesBegan方法,[UIAppllication shareApplication]


方法1 [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];

1、第一步

- (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
   
    //创建self.view上的各种控件。。。。

    

    //创建完所有控件后,在此添加手势 保证加在self.view的最外层

   //设置点击任何其他位置 键盘回收

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBG:)];
    [self.view addGestureRecognizer:tapGesture];
}


2、第二步 手势action 调用UIApplication sharedApplication

- (void)tapBG:(UITapGestureRecognizer *)gesture {
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}

即可实现点击textField外任意区域回收键盘。。。

0 0
原创粉丝点击