2

我一直在試圖解決這個問題一兩天了,我承認我需要幫助。標籤導航視圖的正確設計模式?

我的應用程序的根視圖控制器是一個標籤欄控制器。我希望每個標籤欄都有一個不同的導航控制器。這些導航控制器的行爲完全不同。

那麼我怎麼設置這個類的方面呢?根據蘋果公司的文檔,我不應該劃分UINavigationViewController。那麼我在哪裏放置驅動每個導航控制器的代碼?這一切都被扔進App Delegate嗎?這將創造一個不可能的混亂。

此應用應該在iOS 4.0或更高版本上運行。 (實際上,我可能需要iOS 4.2。)

回答

2

這取自我的一個應用程序。正如你所說,你不應該子類UINavigationController,而是你使用它們,你可以在UINavigationController's上添加viewcontroller。然後,在設置每個UINavigationController中的根視圖控制器之後,將UINavigationController添加到UITabBarController(phew!)。

因此,每個選項卡將「指向」一個UINavigationController,它具有一個常規viewcontroller作爲根視圖控制器,它是根視圖控制器(您添加的視圖控制器),當用(可選)導航欄在頂部。

UITabBarController *tvc = [[UITabBarController alloc] init]; 
    self.tabBarController = tvc; 
    [tvc release]; 

    // Instantiates three view-controllers which will be attached to the tabbar. 
    // Each view-controller is attached as rootviewcontroller in a navigationcontroller. 

    MainScreenViewController *vc1 = [[MainScreenViewController alloc] init]; 
    PracticalMainViewController *vc2 = [[PracticalMainViewController alloc] init]; 
    ExerciseViewController *vc3 = [[ExerciseViewController alloc] init]; 

    UINavigationController *nvc1 = [[UINavigationController alloc] initWithRootViewController:vc1]; 
    UINavigationController *nvc2 = [[UINavigationController alloc] initWithRootViewController:vc2]; 
    UINavigationController *nvc3 = [[UINavigationController alloc] initWithRootViewController:vc3]; 

    [vc1 release]; 
    [vc2 release]; 
    [vc3 release]; 

    nvc1.navigationBar.barStyle = UIBarStyleBlack; 
    nvc2.navigationBar.barStyle = UIBarStyleBlack; 
    nvc3.navigationBar.barStyle = UIBarStyleBlack; 

    NSArray *controllers = [[NSArray alloc] initWithObjects:nvc1, nvc2, nvc3, nil]; 

    [nvc1 release]; 
    [nvc2 release]; 
    [nvc3 release]; 

    self.tabBarController.viewControllers = controllers; 

    [controllers release]; 

這是我從一個視圖 - 控制到另外一個(這是由在實現代碼如下輕敲細胞做,但你看到可以使用pushViewController方法無論你想)。

(這是從應用程序的另一部分截取)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (self.detailedAnswerViewController == nil) { 
     TestAnsweredViewController *vc = [[TestAnsweredViewController alloc] init]; 
     self.detailedAnswerViewController = vc; 
     [vc release]; 
    } 

    [self.navigationController pushViewController:self.detailedAnswerViewController animated:YES]; 
} 

self.navigationcontroller屬性當然在其上的的UINavigationController層次結構被推每個視圖控制器設置的。

+0

如何管理PracticalMainViewController和PracticalSecondaryViewController之間的交互?關鍵值觀察? –

+0

在我的情況下,PracticalMainViewController是一個UITableView,我在didSelectRowAtIndexPath中使用[self.navigationController push ...]來推動一個新的viewcontroller;這是回答這個問題還是你的意思是我如何在它們之間傳遞數據? – LuckyLuke

+0

啊。它可以提供一個委託或任何其他。聰明。謝謝。 –