检测UITextField的焦点更改 [英] Detect Focus Change for UITextField

查看:88
本文介绍了检测UITextField的焦点更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置动画,以便在隐藏和显示文本字段的键盘时向上移动视图,并使它工作得很好,但是当焦点从一个文本字段移到另一个文本字段时,它并没有由于已经显示了键盘,因此无法正常工作.

I'm trying to set the animation for the view to move up when keyboard is hiding and appearing for the text fields and I got it to work perfectly fine, but when the focus moves from one text field to another, it doesn't work since the keyboard was already shown.

在viewDidLoad中,我注册了以下内容:

In viewDidLoad, I registered the following:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

,然后在keyboardWillShow和keyboardWillHide方法中,它确定视图是否应该移动并相应地设置动画.但是,如果已经显示了键盘,并且用户单击了需要向上移动视图的另一个文本字段,则不会调用该方法.当已经显示键盘时,是否有任何方法可以检测焦点是否已更改为另一个文本字段?如果有一种方法可以这样做,而不必将所有文本字段都设置为委托,那就太好了.

and then in the keyboardWillShow and keyboardWillHide methods, it determines if the view should move or not and animate accordingly. But if a keyboard was already shown and the user clicks on another text field that needs the view to move up, the method wouldn't get called. Is there any way to detect if a focus has been changed to another text field when the keyboard was already shown? It would be great if there is a way to do this without having to set all the text fields to delegates.

先谢谢了.

推荐答案

使用UITextField委托方法..在您的情况下,它比键盘方法要好..当textField获得焦点时,将触发- (void)textFieldDidBeginEditing:(UITextField *)textField;.当它失去焦点时会触发- (void)textFieldDidEndEditing:(UITextField *)textField;.

Use the UITextField delegate methods .. its better on your case than the keyboard methods .. when textField got focus the - (void)textFieldDidBeginEditing:(UITextField *)textField; will be fired .. and when it lost focus - (void)textFieldDidEndEditing:(UITextField *)textField; will be fired.

这篇关于检测UITextField的焦点更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆