在xcode故事板中隐藏键盘

时间:2012-11-19 19:40:38

标签: objective-c ios

新的xcode,我在xcode 4.2中创建一个简单的登录表单,我想隐藏键盘,我想有正确的代码,从教程中我说我需要将视图的类更改为UIControl但没有选择,在使用故事板时还有另一种方法吗?

- (IBAction)backGroundTouched:(id)sender
{
    [emailTextField resignFirstResponder];
    [passTextField resignFirstResponder];
}

3 个答案:

答案 0 :(得分:5)

假设您在viewCotroller中执行它们,请调用

[self.view endEditing:YES];

答案 1 :(得分:0)

Make sure your both text fields is connect with it's IBOutlets.
No need to change UIView to UIControl.

// Connect every textfield's "Did end on exit" event with this method.
-(IBAction)textFieldReturn:(id)sender
{
    [sender resignFirstResponder];


}

// Use this method also if you want to hide keyboard when user touch in background

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [emailTextField resignFirstResponder];
    [passTextField resignFirstResponder];
}

答案 2 :(得分:0)

我遵循了本教程:http://www.techotopia.com/index.php/Writing_iOS_7_Code_to_Hide_the_Keyboard并且它对我有用:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

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