我有一个应用程序,通过让用户点击一个编辑按钮,在表格底部显示一个文本字段单元格,将新项目添加到表格 View 中,类似于内置的通知应用程序。当显示键盘时,我需要调整表格,以便当表格中有很多行时键盘不会受到阻碍。我通过订阅键盘显示时的通知来执行此操作:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector (keyboardDidShow:)
name: UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector (keyboardDidHide:)
name: UIKeyboardDidHideNotification
object:nil];
}
...
...
-(void) keyboardDidShow: (NSNotification *)notif
{
// If keyboard is visible, return
if (self.keyboardVisible)
{
return;
}
// Get the size of the keyboard.
NSDictionary* info = [notif userInfo];
NSValue* aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
// Adjust the table view by the keyboards height.
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0);
NSIndexPath *path = [NSIndexPath indexPathForRow:self.newsFeeds.count inSection:0];
[self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated:YES];
self.keyboardVisible = YES;
}
但是,我让用户添加行的表也可以被点击,并将新 View 推送到应用程序。该 View 还有一个 TextView ,当用户点击它并且键盘显示时,第一个 View Controller 仍然会收到通知,这会导致崩溃。
如何忽略通知或使其在推送新 View 时不触发?
最佳答案
您可以在 viewDidAppear 中将该类添加为观察者,并在 viewWillDisappear 中将其删除。
关于ios - 如何在多个 View 上处理keyboardDidShow?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12443292/