2010-06-09 25 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

相關問題