以编程方式重新启动iPhone键盘

时间:2011-12-20 06:52:50

标签: iphone keyboard iphone-softkeyboard

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

4 个答案:

答案 0 :(得分:4)

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

[textfield resignFirstResponder];

答案 1 :(得分:1)

您是否包含该方法:

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

答案 2 :(得分:1)

[[[UIApplication sharedApplication] keyWindow] endEditing:YES];将为您效劳。

答案 3 :(得分:0)

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