我必須在這裏丟失一些基本的東西。我有一個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];
}
你可以發佈你的代碼爲你的tableView:didSelectRowAtIndexPath:選擇器? – randombits 2010-06-10 01:39:06