使用storyboard建立的TextField代码,需要在头文件中建立委托:
@interface MyViewViewController : UIViewController<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *myTextField;
@end
在viewDidLoad方法中加入下列代码:
self.myTextField.delegate = self;
'
一、点击return回收键盘:
实现textFieldShouldReturn方法:
-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
二、点击空白处回收键盘:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.myTextField resignFirstResponder];
}