UITextField&的问题iOS中的键盘管理 [英] Problems with UITextField & Keyboard Management in iOS

查看:133
本文介绍了UITextField&的问题iOS中的键盘管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我目前的目标是iOS 2.0.)

(I'm targetting iOS 2.0 for the time being.)

我有一个继承UITextField并实现UITextFieldDelegate协议的类.

I have a class which subclasses UITextField and implements the UITextFieldDelegate protocol.

首先,我没有将类设置为其自己的委托,并且当我单击该字段时,键盘会自动弹出.到目前为止,一切都很好.

At first, I did not set the class as its own delegate, and when I clicked in the field, the keyboard popped up automatically. So far, so good.

我使用UIReturnKeyDone发送setReturnKeyType:消息,因此它将右下角的按钮更改为完成".

I send the setReturnKeyType: message with UIReturnKeyDone, so it changes the bottom-right button to say Done.

问题是,当我单击完成"或按[返回]时,什么也没发生-键盘不会消失.

The problem was, when I clicked Done or pressed [Return], nothing happened - the keyboard wouldn't go away.

我尝试将self作为观察者添加到结束编辑通知中,但是当单击Done时,它从未被调用;在Google搜索中,似乎只有在该领域辞去第一响应者后才会被解雇-这是我无法实现的目标.

I tried adding self as an observer for the end editing notification, but it never got called when Done was clicked; from a Google search it seems that only gets fired when the field resigns as the first responder - which is the bit I can't get to happen.

然后我在这里找到答案,建议将self添加为代理并处理textFieldShouldReturn:.问题是,一旦我添加self作为代理,单击该字段时键盘就不再弹出(并且它没有获得焦点)-看来它并没有成为第一响应者.

I then found answers on here that suggested adding self as the delegate and handling textFieldShouldReturn:. The problem is, as soon as I add self as the delegate, the keyboard no longer pops up when you click the field (and it doesn't gain focus) - it seems it isn't becoming first responder.

我尝试处理textFieldShouldBeginEditing:并返回YES(文档说如果不被覆盖,则应为默认值),并且被调用,但没有区别.由于无知,我尝试了[textField成为FirstResponder],并因堆栈溢出而得到奖励(现在我知道响应 try 成为第一个响应者而调用了它).

I tried handling textFieldShouldBeginEditing: and returning YES (docs say that should be the default if not overridden), and that got called, but made no difference. In my ignorance I tried [textField becomeFirstResponder] and was rewarded with a stack overflow (now I know that is called in response to trying to become first responder).

我现在完全被卡住了!

I'm now thoroughly stuck!

任何人都可以帮忙吗?我只希望当用户单击完成"或按[Return]时键盘消失.

Can anyone help? I just want the keyboard to go away when the user clicks Done or presses [Return].

推荐答案

我的错.事实证明,您不应该将文本字段的委托设置为其自身-由于未处理的选择器会转发给其委托,因此会带来一些麻烦.如果它不响应选择器,则消息将转到其委托人……这就是它本身,我们已经知道它不会响应.因此,无限循环.

My fault. Turns out you're not supposed to set a text field's delegate to itself - there are complications due to unhandled selectors being forwarded to its delegate. If it doesn't respond to a selector the message goes to its delegate... which is itself, which we already know doesn't respond. Hence, an infinite loop.

使用一个单独的对象作为代理时,效果很好.

Using a separate object as the delegate worked perfectly.

希望这可以帮助其他人避免同样的问题.

Hopefully this will help someone else avoid the same problem.

这篇关于UITextField&的问题iOS中的键盘管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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