让控制器监听键盘的通知,注意谁监听,谁的dealloc方法中就要remove,如果非ARC还要调用父类的dealloc方法。
//监听键盘的操作:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)keyboardWillChangeFrame:(NSNotification *)note{
NSLog(@"发送者%@ 内容%@",note.name,note.userInfo);
}
当键盘弹出时,接收到的内容为一个字典:需要注意的是其中的Key都有定义好的NSString,可以直接使用。
{
UIKeyboardAnimationCurveUserInfoKey = 7; //动画的执行节奏
UIKeyboardAnimationDurationUserInfoKey = "0.25"; //动画时长
UIKeyboardBoundsUserInfoKey = "NSRect: {
{0, 0}, {320, 224}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 592