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

啊。它可以提供一个委托或任何其他。聪明。谢谢。 –