【问题标题】:Resign the iPhone keyboard programmatically以编程方式退出 iPhone 键盘
【发布时间】:2011-12-20 06:52:50
【问题描述】:

在我的创建个人资料中,我只有两个文本字段:体重和出生日期。当用户触摸重量时,键盘会显示。但是当用户触摸出生日期时,日期选择器会出现在操作表中。当用户选择日期并按下完成按钮时,操作表消失但键盘保持打开状态。而且没有办法隐藏这个键盘。我使用了 resignFirstResponder 方法,但没有运气。

【问题讨论】:

  • 你试过什么?贴一些代码。 resignFirstResponder 是正确的做法。
  • 通过单击箭头将正确答案单击为正确。它将帮助您将来从 SO 获得持续的帮助。谢谢。

标签: iphone keyboard iphone-softkeyboard


【解决方案1】:

当你想隐藏键盘时需要这样做:

[textfield resignFirstResponder];

【讨论】:

  • 您确定要辞职的文本字段正确吗?可能是 textfield 变量为 nil,或者指向与所选文本字段不同的文本字段。在调用 resignFirstResponder 之前尝试使用 NSLogo 记录文本字段的值。
  • 哦,我忘了[txtField becomeFirstResponder]
【解决方案2】:

你是否包含了方法:

-(BOOL) textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    if ([txtComment isFirstResponder] && [touch view] != txtComment)
    {
        [txtComment resignFirstResponder];
    }
    [super touchesBegan:touches withEvent:event];
}

【讨论】:

  • 这两种方法我都用过。哦,我忘记了 [txtField becomeFirstResponder] 现在它工作正常。感谢您的支持
【解决方案3】:

[[[UIApplication sharedApplication] keyWindow] endEditing:YES]; 将为您工作。

【讨论】:

    【解决方案4】:
    -(void) ViewDidLoad
    {
    
     // your some codes
    
       UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
       [self.view addGestureRecognizer:gestureRecognizer];
       gestureRecognizer.cancelsTouchesInView = NO;
    }
    
    - (void) hideKeyboard 
    {
        [textfiledname1 resignFirstResponder];
        [textfieldname2 resignFirstResponder];
    }
    

    【讨论】:

      猜你喜欢
      • 2012-09-17
      • 2015-07-12
      • 2011-02-04
      • 2012-07-05
      • 2010-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多