禁用键盘

发布于 2024-07-29 16:18:37 字数 25 浏览 4 评论 0原文

如何通过按 Return 键隐藏键盘

How to Hide Keyboard by pressing Returnkey

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

冧九 2024-08-05 16:18:37

您需要记住几件事。 开发人员忘记设置的第一个部分是文本字段的委托

如果您使用 Interface Builder,则必须记住,您需要将 textField 的委托设置为文件所有者。

替代文本 http://www.thoughtblog.com/imgs/delegate.png

如果如果您没有使用 Interface Builder,那么请确保将文本字段的委托设置为 self。 我还包括了 returnType。 例如,如果 textField 名为 gameField:

gameField.delegate = self;
gameField.returnType = UIReturnKeyDone;

您还必须为 ViewController 实现 UITextFieldDelegate

@interface YourViewController : UIViewController <UITextFieldDelegate> 

最后,您需要使用 textFieldShouldReturn 方法并调用 [textField resignFirstResponder]

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

您的所有文本字段都将使用相同的方法,因此您只需进行一次此设置。 只要为textField设置了delegate,就为界面实现了UITextFieldDelegate,添加textFieldShouldReturn方法并调用
resignFirstResponder 你的集合。

There is a couple of things you need to remember. The number #1 part developers forget to set is the delegate of the textField.

If you are using the Interface Builder, you must remember that you need to set the delegate of the textField to the file Owner.

alt text http://www.thoughtblog.com/imgs/delegate.png

If you are not using Interface Builder then make sure you set the delegate of the textfield to self. I also include the returnType. For Example if the textField was called gameField:

gameField.delegate = self;
gameField.returnType = UIReturnKeyDone;

You must also implement the UITextFieldDelegate for your ViewController.

@interface YourViewController : UIViewController <UITextFieldDelegate> 

Finally you need to use the textFieldShouldReturn method and call [textField resignFirstResponder]

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

All your textFields will use this same method so you only need to have this setup once. As long as the delegate is set for the textField, the UITextFieldDelegate is implemented for the interface, you add the textFieldShouldReturn method and call the
resignFirstResponder your set.

∞觅青森が 2024-08-05 16:18:37

仅当可编辑的内容(通常是 UITextField)成为第一响应者时,键盘才会显示。 因此,要使键盘消失,您必须使 textField 不再是第一个响应者。 幸运的是,只有一行代码:

[myTextField resignFirstResponder];

The keyboard only shows up when something editable (usually a UITextField) has become the first responder. Therefore, to make the keyboard go away, you have to make the textField not be the firstResponder anymore. Fortunately, it's one line of code:

[myTextField resignFirstResponder];
温柔少女心 2024-08-05 16:18:37

您确实需要在问题中包含更多信息,但我认为这可能就是您正在寻找的:

首先,让您的视图控制器实现 UITextFieldDelegate:

@interface MainViewController : UIViewController <UITextFieldDelegate> {

然后将此方法添加到控制器:

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

阅读 UITextFieldDelegate 文档 查看您还可以做什么。

You really need to include more information with your question, but I think this might be what you are looking for:

First, make your view controller implement the UITextFieldDelegate:

@interface MainViewController : UIViewController <UITextFieldDelegate> {

Then add this method to the controller:

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

Read the UITextFieldDelegate documentation to see what else you can do.

爱你不解释 2024-08-05 16:18:37

使用这两种方法:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
        activeTextField = textField;   
        return YES;
  }

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
        activeTextField = nil; 
        [txtPassword resignFirstResponder];
        [txtUserName resignFirstResponder];
        return YES;
  }

请确保您已为每个文本字段指定了委托。 为此,你应该去看风景。 右键点击 。 设置委托查看。

Use these two methods:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
        activeTextField = textField;   
        return YES;
  }

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
        activeTextField = nil; 
        [txtPassword resignFirstResponder];
        [txtUserName resignFirstResponder];
        return YES;
  }

Please make sure you have given delegates to each textfields. For that you should go to the view. Right click . set the delegate to view.

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