关闭键盘方法总结

1.

UITextField nameField;  
nameField.delegate = self;  
//实现重写了textFieldShouldReturn:方法  
-(BOOL)textFieldShouldReturn:(UITextField *)textField  
{  
    [textField resignFirstResponder];  
    return YES;  
}

2.触摸空白关闭键盘

// 触摸背景,关闭键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    UIView *view = (UIView *)[touch view];
    if (view == self.view) {
        [passwordFeild resignFirstResponder];
    }
}
 
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view  endEditing:NO];//注:view为TextField的背景View
}
 

3.所有键盘返回

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField  resignFirstResponder];//所有键盘返回
    //[passwordFeild  resignFirstResponder];//指定textfield
    return YES;
}

 4.下一项  最后关闭键盘

- (BOOL)textFieldShouldReturn:(UITextField*)textField
{
    if (textField==usernameField) {
        [passwordFeild  becomeFirstResponder];
    }else{
        [passwordFeild  resignFirstResponder];
    }
    return YES;
}

 

猜你喜欢

转载自huluo666.iteye.com/blog/2086423