繁体   English   中英

以编程方式退出iPhone键盘

[英]Resign the iPhone keyboard programmatically

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

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

[textfield resignFirstResponder];

您是否包含方法:

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

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

-(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