iPhone:可以使用 UITextField 清除按钮关闭键盘吗?
我想知道是否有办法让 UITextField 清除按钮“始终可见”
textfield.clearButtonMode = UITextFieldViewModeAlways;
似乎不起作用 ..是否可以使用按钮关闭键盘?
提前致谢。
I'm wondering if there is a way to have the UITextField clear button 'always visible'
textfield.clearButtonMode = UITextFieldViewModeAlways;
doesn't seem to work
.. and if it is possible to dismiss the keyboard using the button?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
就像之前提到的,苹果似乎在清除字段后设置文本字段焦点。
解决方案非常简单。自己清空字段即可,resignFirstResponder并返回NO
Like mentioned before it seems apple is setting the textfield focus after you clear the field.
The solution is quite simple. Just clear the field yourself, resignFirstResponder and return NO
该函数
在您的委托中,当用户想要清除文本字段时调用 。如果你返回 YES 并调用
键盘应该消失。我不知道clearButtonMode,除了您可能想尽早设置它,最好是在将视图添加到其超级视图之前。
编辑为了确保您确实辞去了响应者的职务,请稍后尝试执行此操作:
In your delegate, the function
is called when the users wants to clear the textfield. If you return YES and call
the keyboard should go away. I don't know about the clearButtonMode, other than that you may want to set it early, preferably before adding the view to its superview.
edit To make sure you really resign the responder, try doing it just a little later:
延迟对我来说效果不太好。相反,我向委托添加了一个实例变量:
然后在委托实现中:
The delay didn't work well for me. Instead I added an instance variable to the delegate:
Then in the delegate implementation:
UITextFieldDelegate textFieldShouldClear
http://developer.apple.com/iphone/library/documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html#//apple_ref/occ/intfm/UITextFieldDelegate/textFieldShouldClear:
UITextFieldDelegate textFieldShouldClear
http://developer.apple.com/iphone/library/documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html#//apple_ref/occ/intfm/UITextFieldDelegate/textFieldShouldClear:
我发现这种奇怪的行为是由竞争手势识别器引起的,该手势识别器在调用 textFieldShouldClear: 之前放弃了键盘之前的第一响应者。它似乎正在腐蚀第一响应者。
如果您以这种方式进行设置,请确保手势识别器上的cancelsTouchesInView 设置为YES。这样您就不需要在 textFieldShouldClear: 或 textFieldShouldBeginEditing: 委托方法中执行任何特殊操作。
I discovered this odd behavior was caused by a competing gesture recognizer that resigned the first responder before the keyboard before textFieldShouldClear: was called. It seemed to be corrupting the first responder.
If you've set it up this way, ensure that cancelsTouchesInView on your gesture recognizer is set to YES. This way you shouldn't need to do anything special in the textFieldShouldClear: or textFieldShouldBeginEditing: delegate methods.