标签导航视图的正确设计模式?

时间:2011-07-06 18:06:47

标签: iphone ios design-patterns uinavigationcontroller uitabbarcontroller

我已经被困了几天试图解开这个问题,我承认我需要帮助。

我的应用程序的根视图控制器是一个标签栏控制器。我想让每个标签栏都有一个不同的导航控制器。这些导航控制器具有完全不同的行为。

那么如何根据课程设置呢?根据Apple的文档,我不应该继承UINavigationViewController。那么我在哪里放置驱动每个导航控制器的代码?这一切都被App Delegate抛出了吗?这将造成一个不可能的混乱。

此应用应在iOS 4.0或更高版本上运行。 (实际上,我可能需要iOS 4.2。)

1 个答案:

答案 0 :(得分:2)

这取自我的一个应用程序。正如您所说,您不应该继承UINavigationController,而是按原样使用它们,并在UINavigationController's上添加viewcontroller。然后在每个UINavigationController中设置根视图控制器后,将UINavigationController添加到UITabBarController(p!)。

因此每个选项卡将“指向”UINavigationController,它具有常规视图控制器作为根视图控制器,它是根视图控制器(您添加的控制器),当按下选项卡时将显示(可选)顶部的导航栏。

    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];

这是我从一个视图控制器转到另一个视图控制器的方法(这是通过在tableview中点击一个单元格来完成的,但正如你所看到的,可以在任何你想要的地方使用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];
}

{I}属性当然是在每个viewController上设置的,它们被推送到UINavigationController层次结构上。