关闭虚拟键盘,有两种方法。
1.按虚拟键盘上的return键关闭。
@interface TestView : UIView<UITextFieldDelegate>
@end
@implementation TestView
- (id)initWithFrame:(CGRect)frame
{
if(self = [super initWithFrame:frame]){
UITextField * test = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 50)];
test.tag = 10;
test.delegate = self;
[self addSubview:test];
[test release];
}
return self;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
UITextField * test = (UITextField *)[self viewWithTag:10];
[test resignFirstResponder];
return YES;
}
@end
2.按屏幕空白处关闭。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITextField * test = (UITextField *)[self viewWithTag:10];
[test resignFirstResponder];
}