简体   繁体   English

以编程方式退出iPhone键盘

[英]Resign the iPhone keyboard programmatically

In my create profile I have only two text fields: weight and date of birth. 在我的创建个人资料中,我只有两个文本字段:体重和出生日期。 When the user touches for weight the keyboard shows. 当用户触摸重量时,将显示键盘。 But when the user touches the date of birth a date picker appears in the action sheet. 但是,当用户触摸生日时,操作表中会出现一个日期选择器。 When the user selects the date and press the done button the action sheet disappears but the keyboard remains open. 当用户选择日期并按下完成按钮时,操作表消失,但键盘保持打开状态。 And there is no way to hide this keyboard. 而且没有办法隐藏此键盘。 I have used resignFirstResponder method but no luck. 我使用过resignFirstResponder方法,但是没有运气。

要隐藏键盘时,需要执行以下操作:

[textfield resignFirstResponder];

Did you include the method : 您是否包含方法:

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

    [textField resignFirstResponder];
    return YES;
}

or 要么

- (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];
}

[[[UIApplication sharedApplication] keyWindow] endEditing:YES]; will work for you. 将为您工作。

-(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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM