初始部分:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil];
一些方法:
- (void) keyboardWillShow:(NSNotification*) aNotification {
// TO DO
}
Dealloc部分:
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
所以在iOS 6.1中呈现视图后没有调用keyboardWillShow …
在iOS 6.0中,此代码完美运行.
最佳答案 “init部分”是被添加的观察者?例如,如果您的视图控制器来自故事板,那么它应该在 – (id)initWithCoder:(NSCoder *)解码器中.
不过,我的建议是在viewWillAppear中设置观察者并在viewWillDisappear中删除它们.这样,设置和拆卸是“平衡的”,仅在视图控制器的内容可见时才有效.