2010-06-09 50 views
0

我必须在这里丢失一些基本的东西。我有一个NavigationViewController里面的UITableView。当在UITableView中选择一个表格行(使用tableView:didSelectRowAtIndexPath :)时,我调用pushViewController来显示一个不同的视图控制器。新的视图控制器显示正确,但是当我弹出视图控制器并返回UITableView被调整大小,如果键盘正在显示。我需要找到一种方法在按下视图控制器之前隐藏键盘,以便正确恢复帧。如果我将代码注释掉以推送视图控制器,则键盘将正确隐藏,并且框架正确调整大小。当推新视图控制器和键盘隐藏时,TableView框架不能正确调整大小

的代码我使用来显示键盘如下:使用

- (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

你可以发布你的代码为你的tableView:didSelectRowAtIndexPath:选择器? – randombits 2010-06-10 01:39:06

回答

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,你可以更容易地手动操纵表视图的框架 - 至少这是我的经验。

+0

感谢您的回复。我已经在UIView上使用了一个类别来查找和退出第一个响应者。我还使用常规的UIViewController,并在键盘显示和隐藏时调整tableview的大小。 – Pete 2010-06-10 11:12:32

+0

嗯。对于那个很抱歉。确信这是问题所在。如何在-viewWillAppear中调整表格视图的大小?你可以先检查它的当前大小,看它是否需要调整大小,或者只是暴力强制它,并调整视图即将出现的时间? – 2010-06-10 17:13:25

相关问题