以编程方式使键盘消失
我创建了一个文本字段,但没有使用 Interface Builder,而是在 Xcode 中以编程方式完成。所以现在我需要一种编程方式来让它退出第一响应程序,以便当用户按下 Enter 时键盘就会消失。
I made a text field but I didn't use Interface Builder, I did it programmatically in Xcode. So now I need a programmatic way to make it resign first responder so that the keyboard will go away when the user presses enter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
docs
如果你希望它在按下 Enter 时消失,则需要
在 UITextFieldDelegate
docs
if you want it to go away when enter is pressed, you will need to implement
in your UITextFieldDelegate
除了cobbal的答案之外,不要忘记将文本字段的委托设置为实现向该类
接口声明添加描述符的类也是一件好事。
As addition to cobbal's answer, don't forget to set text field's delegate to the class that implements
Adding descriptor to that class interface declaration is also a good thing.
我自己也有这个问题,我意识到上面的答案可能不是你要问的。我的猜测是,当您“说双击”(或其他一些事件)时,您会将 UITextField 作为子视图添加到某些视图中。让控制器成为该子视图的委托非常重要。
在本例中,我使用了控制器本身。
现在在该控制器内我可以实现
并且它将起作用。
I had this question my self and I realize that the above answer may not be what you're asking. My guess is you are adding UITextField as subviews to some view when you 'say double tap' (or some other event). It is important to make a controller the delegate of that subview.
In this case I've used the controller itself.
Now inside that controller I can implement
and it will work.