我有一个应用程序,通过让用户点击一个在表格底部显示一个文本字段单元格的编辑按钮,我向表格视图添加一个新项目,类似于内置的通知应用程序。我需要在显示键盘时调整表格,以便在表格中有很多行时不会阻碍它。我通过订阅键盘显示时的通知来做到这一点:
- (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;
}
但是,我让用户添加行的表也可以被点击,并且新视图被推送到应用程序。这个视图也有一个文本视图,当用户点击它并且键盘显示第一个视图控制器时仍然会收到通知,这会导致崩溃。
如何在推送新视图时忽略通知或使其不触发?