我已經在Apple文檔以及類似的堆棧溢出問題有一個很好的看看,但我堅持爲什麼我的navigationController在使用標籤欄時爲空。我正在嘗試從代碼構建大部分應用程序,而不是使用XIB插入navigationController。UINavigationController爲NULL
在調試過程中,我大大簡化了我的應用程序,直到兩個選項卡。一個選項卡包含一個tableview,當一行被觸摸時,我期待一個詳細頁面(來自XIB)出現。應該很簡單。我試圖推送詳細信息視圖時發現self.navigationController的值爲NULL,當然它不起作用。我完全採用了標籤欄,它在單個視圖(tableview)中正常工作。在這個例子中,self.navigationController有一個值。
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// With Tab Bars
self.tabBarController = [[UITabBarController alloc] init];
ViewController *vc1 = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
vc1.tabBarItem.title = @"Words";
vc1.tabBarItem.image = [UIImage imageNamed:@"tab_feed.png"];
TextTableViewController *vc2 = [[TextTableViewController alloc] init];
vc2.tabBarItem.title = @"Text";
vc2.tabBarItem.image = [UIImage imageNamed:@"tab_live.png"];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:vc1];
NSArray* controllers = [NSArray arrayWithObjects:vc2, navController, nil];
tabBarController.viewControllers = controllers;
tabBarController.delegate = self;
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.rootViewController = self.tabBarController;
[window makeKeyAndVisible];
return YES;
}
從TextTableViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TextViewController *detailViewController = [[TextViewController alloc] initWithNibName:@"TextViewController" bundle:nil];
Text *text = [[Text alloc] init];
text = [textArray objectAtIndex:indexPath.row];
detailViewController.TextID = text.textID;
NSLog(@"Nav Controller: %@",self.navigationController);
[self.navigationController pushViewController:detailViewController animated:YES];
NSLog(@"pushed");
}
我也有兩個與此問題相關的問題。 (1)這條線的目的是什麼。它看起來沒有什麼區別,如果它處於或不在,並且不在Apple示例中。
tabBarController.delegate = self;
(2)當創建一個標籤數組時,其中一個視圖被製作成navigationController。它與哪個選項卡有關係嗎?或者應該是不同的視圖,這些視圖完全與任何選項卡無關並且不可見。問題在哪裏?
你會創建一個'UINavigationController'數組,然後用'setViewControllers:' – sooper 2012-07-08 23:54:47
將它添加到你的'UITabBarController'謝謝scooper我會檢查出來。 – ardochhigh 2012-07-09 20:38:28