【问题标题】:Disabling Keyboard禁用键盘
【发布时间】:2009-07-24 10:39:28
【问题描述】:

如何按回车键隐藏键盘

【问题讨论】:

标签: iphone cocoa-touch iphone-softkeyboard


【解决方案1】:

您需要记住几件事。开发人员忘记设置的编号 #1 部分是 textField 的 delegate

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

alt text 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 你的集合。

【讨论】:

    【解决方案2】:

    只有当可编辑的东西(通常是 UITextField)成为第一响应者时,键盘才会出现。因此,要使键盘消失,您必须使 textField 不再是 firstResponder。幸运的是,这是一行代码:

    [myTextField resignFirstResponder];
    

    【讨论】:

      【解决方案3】:

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

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

      @interface MainViewController : UIViewController <UITextFieldDelegate> {
      

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

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

      阅读UITextFieldDelegate documentation 了解您还能做什么。

      【讨论】:

        【解决方案4】:

        使用这两种方法:

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

        请确保您已为每个文本字段分配了代表。为此,您应该去看风景。右键点击 。将委托设置为查看。

        【讨论】:

          猜你喜欢
          • 2017-01-05
          • 2023-03-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-06
          • 2023-04-02
          相关资源
          最近更新 更多