我必须遗漏一些基本的东西。我在NavigationViewController中有一个UITableView。当在UITableView中选择一个表行时(使用tableView:didSelectRowAtIndexPath :)我调用pushViewController来显示一个不同的视图控制器。新的视图控制器正确显示,但当我弹出该视图控制器并返回时,UITableView的大小调整大小,就好像键盘正在显示一样。在推动视图控制器之前,我需要找到一种让键盘隐藏的方法,以便正确恢复帧。如果我注释掉代码以推动视图控制器,则键盘会正确隐藏,并且框架会正确调整大小。
我用来显示键盘的代码如下:
- (void) keyboardDidShowNotification:(NSNotification *)inNotification {
NSLog(@"Keyboard Show");
if (keyboardVisible) return;
// We now resize the view accordingly to accomodate the keyboard being visible
keyboardVisible = YES;
CGRect bounds = [[[inNotification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
bounds = [self.view convertRect:bounds fromView:nil];
CGRect tableFrame = tableViewNewEntry.frame;
tableFrame.size.height -= bounds.size.height; // subtract the keyboard height
if (self.tabBarController != nil) {
tableFrame.size.height += 48; // add the tab bar height
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(shrinkDidEnd:finished:contextInfo:)];
tableViewNewEntry.frame = tableFrame;
[UIView commitAnimations];
}
使用以下方法隐藏键盘:
- (void) keyboardWillHideNotification:(NSNotification *)inNotification {
if (!keyboardVisible) return;
NSLog(@"Keyboard Hide");
keyboardVisible = FALSE;
CGRect bounds = [[[inNotification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
bounds = [self.view convertRect:bounds fromView:nil];
CGRect tableFrame = tableViewNewEntry.frame;
tableFrame.size.height += bounds.size.height; // add the keyboard height
if (self.tabBarController != nil) {
tableFrame.size.height -= 48; // subtract the tab bar height
}
tableViewNewEntry.frame = tableFrame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(_shrinkDidEnd:finished:contextInfo:)];
tableViewNewEntry.frame = tableFrame;
[UIView commitAnimations];
[tableViewNewEntry scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionMiddle animated:YES];
NSLog(@"Keyboard Hide Finished");
}
我通过让第一个响应者为ViewWillDisappear中的第一个响应者控制来触发键盘被隐藏。我添加了NSLog语句并查看日志文件中发生的事情,如下所示:
显示键盘
ViewWillDisappear:隐藏键盘
隐藏键盘
键盘隐藏完成
PushViewController(我推动新视图控制器时的NSLog条目)
从这个跟踪中,我可以看到正确顺序发生的事情,但似乎在按下视图控制器时键盘隐藏代码无法正常执行。
任何想法都会非常感激。我一直在敲击键盘一段时间试图找出我做错了什么。
- 添加了didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.section) {
case 0: // Required Info
// removed to simplify
case 1: // Optional Info
switch (indexPath.row) {
case 0:
[self showTextDetailPicker: @"Enter a description"
tag: tDescriptionPicker
sourceTarget: self.newRecord
fieldSource: @selector(description)];
break;
default:
break;
}
break;
default:
break;
}
}
- (void) showTextDetailPicker: (NSString*) titleText tag:(int)tagID sourceTarget:(NSObject*)target fieldSource:(SEL)selector{
FieldEditorViewController *fe = [[FieldEditorViewController alloc] init];
fe.titleText = titleText;
fe.fieldText = [target performSelector: selector];
fe.tag = tagID;
// Replace default back button with one that just says 'Back'
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc]
initWithTitle:@"Back"
style:UIButtonTypeInfoLight
target:nil action:nil];
[[self navigationItem] setBackBarButtonItem: newBackButton];
[newBackButton release];
[fe setDelegate: self];
[self.navigationController pushViewController:fe animated:YES];
[fe release];
}
答案 0 :(得分:2)
在推动视图控制器之前,找到第一个响应者并呼叫辞职。使用category from this SO post查看如何递归找到第一个响应者(不确定你是否已经这样做了)。
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIView *fp = [[self view] findFirstResponder];
[fp resignFirstResponder];
// Push new view controller here.
NextViewController *controller = [[NextViewController alloc] init];
[[self navigationController] pushViewController:controller animated:YES];
[controller release], controller = nil;
}
要记住的另一件事是您的表视图会自动调整大小,因为您的根视图控制器是从UITableViewController派生的(或者看起来似乎如此)。如果你使根视图控制器成为一个常规的UIViewController 包含一个UITableView,你可以更容易地手动操作表视图的框架 - 至少这是我的经验。