UIView之endEditing方法

面介绍下endEditing:方法,该方法为UIView中的一个方法,定义为 


1

- (BOOL)endEditing:(BOOL)force

官网文档释义: 

Causes the view (or one of its embedded text fields) to resign the first responder status.

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.


大意为: 

注销当前view(或它下属嵌入的text fields)的first responder 状态。

该方法会在当前view以及其subview层次结构中需找当前处于first responder状态的text field。如果找到的话会注销其first responder状态,如果指定force参数为YES,则不再询问text field,而直接强制注销其first responder状态。


好了,有了这个快捷方法,在FormViewController中,则不需要在实现UITextFieldDelegate,来对处于编辑状态的textField进行跟踪,也不必担心将来会添加N个UITextView,只要是在FormViewController下,我们 

只要调用  

?

1

[self.view endEditing:YES];

键盘立马关闭! 


这段代码加在  控制器里面,可以监听所有的textField和textView  ,方便之处就在于不用谢代理方法去取消第一响应,当控件多的时候会显得比较麻烦


- (void)setKeyBoardAutoHidden{

    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

    //SingleTap Gesture

    UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTapDismissKeyboard:)];

    

    NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];

    

    //UIKeyboardWillShowNotification

    [notificationCenter addObserverForName:UIKeyboardWillShowNotification object:nil queue:mainQueue usingBlock:^(NSNotification *note) {

        [self.view addGestureRecognizer:singleTapGesture];

    }];

    

    //UIKeyboardWillHideNotification

    [notificationCenter addObserverForName:UIKeyboardWillHideNotification object:nil queue:mainQueue usingBlock:^(NSNotification *note) {

        [self.view removeGestureRecognizer:singleTapGesture];

    }];

    

}

- (void) backgroundTapDismissKeyboard:(UIGestureRecognizer *) gestureRecognizer{

    //self.view里所有的subviewfirst responder resign

    [self.view endEditing:YES];

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值