标签式导航视图的正确设计模式? [英] Right design pattern for tabbed navigation views?

查看:209
本文介绍了标签式导航视图的正确设计模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的应用程序的根视图控制器我已经被困在试图解决这几天了,我承认我需要帮助。是一个标签栏控制器。我想让每个标签栏都有不同的导航控制器。这些导航控制器有完全不同的行为。



那么我如何根据课程设置呢?根据Apple的文档,我不应该将 UINavigationViewController 进行子类化。那么在哪里可以放置驱动每个这些导航控制器的代码?这一切都会在App Delegate中抛出?这将造成一个不可能的混乱。



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

解决方案

这是从我的一个应用程序中获取的。正如你所说,你不应该将 UINavigationController 进行子类化,而是按照原样使用它们,并在 UINavigationController的。然后在每个 UINavigationController 中设置根视图控制器后,您将 UINavigationController 添加到 UITabBarController (hew!)。所以每个选项卡都将指向一个具有常规视图控制器作为root viewcontroller的 UINavigationController ,它是根视图控制器(您添加的视图控制器),当顶部使用(可选)导航栏按下选项卡时将显示。

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

//实例化将附加到Tabbar的三个视图控制器。
//每个视图控制器作为rootviewcontroller附加在导航控制器中。

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

self.tabBarController.viewControllers = controllers;

[controllers release];

这是我从一个viewcontroller到另一个viewcontroller(这是通过点击一个单元格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];
}

self.navigationcontroller 属性当然设置在每个视图控制器上,这些都被推送到UINavigationController hierachy上。


I've been stuck trying to puzzle this out for a couple days now, and I'll admit I need help.

The root view controller of my application is a tab bar controller. I want to have each tab bar a different navigation controller. These navigation controllers have completely different behavior.

So how do I set this up in terms of classes? Per Apple's documentation, I'm not supposed to subclass UINavigationViewController. So where do I put the code that drives each of these navigation controllers? Does it all get thrown in App Delegate? That would create an impossible mess.

This app should run on iOS 4.0 or later. (Realistically, I can probably require iOS 4.2.)

解决方案

This is taken from one of my applications. As you say, you are not supposed to subclass UINavigationController, instead you use them as they are and you add viewcontroller on the UINavigationController's. Then after setting the root viewcontroller in each UINavigationController, you add the UINavigationController to the UITabBarController (phew!).

So each tab will "point" to a UINavigationController which has a regular viewcontroller as root viewcontroller, and it is the root viewcontroller (the one you add) that will be shown when a tab is pressed with a (optional) navigationbar at top.

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

This is how I go from one viewcontroller to another one (this is done by tapping a cell in a tableview but as you see the pushViewController method can be used wherever you want).

(this is taken from another part of the app)

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

The self.navigationcontroller property is of course set on each viewcontroller which are pushed on the UINavigationController hierachy.

这篇关于标签式导航视图的正确设计模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆