以编程方式使键盘消失

发布于 2024-09-25 01:37:34 字数 108 浏览 6 评论 0原文

我创建了一个文本字段,但没有使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

荒路情人 2024-10-02 01:37:34
[textField resignFirstResponder];

docs

如果你希望它在按下 Enter 时消失,则需要

- (BOOL)textFieldShouldReturn:(UITextField *)textField

UITextFieldDelegate

[textField resignFirstResponder];

docs

if you want it to go away when enter is pressed, you will need to implement

- (BOOL)textFieldShouldReturn:(UITextField *)textField

in your UITextFieldDelegate

陪你搞怪i 2024-10-02 01:37:34

除了cobbal的答案之外,不要忘记将文本字段的委托设置为实现向该类

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
   [textField resignFirstResponder];
   return YES;    
}

接口声明添加描述符的类也是一件好事。

As addition to cobbal's answer, don't forget to set text field's delegate to the class that implements

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
   [textField resignFirstResponder];
   return YES;    
}

Adding descriptor to that class interface declaration is also a good thing.

烙印 2024-10-02 01:37:34

我自己也有这个问题,我意识到上面的答案可能不是你要问的。我的猜测是,当您“说双击”(或其他一些事件)时,您会将 UITextField 作为子视图添加到某些视图中。让控制器成为该子视图的委托非常重要。

-(void)handleDoubleTapGesture:(UITapGestureRecognizer *)gestureRecognizer{
     //some other code creating the textfield 
     [aTextField setDelegate:self];

在本例中,我使用了控制器本身。

现在在该控制器内我可以实现

-(BOOL) textFieldShouldReturn:(UITxtField *)textField

并且它将起作用。

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.

-(void)handleDoubleTapGesture:(UITapGestureRecognizer *)gestureRecognizer{
     //some other code creating the textfield 
     [aTextField setDelegate:self];

In this case I've used the controller itself.

Now inside that controller I can implement

-(BOOL) textFieldShouldReturn:(UITxtField *)textField

and it will work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文