以编程方式退出 iPhone 键盘

发布于 2024-12-22 04:45:06 字数 160 浏览 3 评论 0原文

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

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.

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

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

发布评论

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

评论(4

十秒萌定你 2024-12-29 04:45:06

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

[textfield resignFirstResponder];

you need to do this when you want to hide the keyboard:

[textfield resignFirstResponder];
南冥有猫 2024-12-29 04:45:06

您是否包括该方法:

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

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];
}
空袭的梦i 2024-12-29 04:45:06

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

[[[UIApplication sharedApplication] keyWindow] endEditing:YES]; will work for you.

微凉徒眸意 2024-12-29 04:45:06
-(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];
}
-(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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文