2012-07-08 28 views
0

我已經在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。它與哪個選項卡有關係嗎?或者應該是不同的視圖,這些視圖完全與任何選項卡無關並且不可見。問題在哪裏?

回答

1

回答有關標籤欄控制器委託的問題(1),請參閱UITabBarControllerDelegate協議參考。對於標籤欄控制器的基本功能,您無需打擾委託人。

但讓我們假設當用戶更改標籤頁時,您希望做一些特殊的事情 - 例如保存文檔或將界面元素重置爲默認值。你可以讓你的一個類,也許你的應用程序委託或另一個控制器類符合UITabBarControllerDelegate協議並實現tabBarController:didSelectViewController:

在你的「答案」中,你問每個標籤是否需要它自己的UINavigation控制器。這是絕對正確的。基本上,每個選項卡都是完全獨立的層次結構,因此每個選項卡中需要一個單獨的UINavigation控制器。

這應該也意味着您的問題(2)在原始帖子中的答案。您需要將導航控制器添加到需要它的特定選項卡。

0

好的我找到了。 UINavigationController需要包含在UITabBarController的相應選項卡中。所以通過改變這個編碼(下面),一個新的UINavigationController被嵌入到帶有tableview的選項卡中。

UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:vc2];        
NSArray* controllers = [NSArray arrayWithObjects:vc1, navController, nil]; 

,然後引出了一個問題:如果你需要的這種多個例子 - 你創建具有需要一個每個標籤一個新的UINavigationController,並標出每一個爲RootViewController的?

+0

你會創建一個'UINavigationController'數組,然後用'setViewControllers:' – sooper 2012-07-08 23:54:47

+0

將它添加到你的'UITabBarController'謝謝scooper我會檢查出來。 – ardochhigh 2012-07-09 20:38:28

相關問題