#import "ViewController.h"
@interface ViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *myTextField;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
self.myTextField.delegate = self;
}
#pragma mark 键盘出现通知
- (void)keyboardWillShow:(NSNotification *)notification {
NSLog(@"键盘出现");
}
#pragma mark 键盘消失通知
- (void)keyboardWillHide:(NSNotification *)notification {
NSLog(@"键盘消失");
}
#pragma mark 点击return收回键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
return [textField resignFirstResponder];
}
#pragma mark 点击编辑区域以外的地方
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
if (![self.myTextField isExclusiveTouch]) {
[self.myTextField resignFirstResponder];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end