如何将TabBar + Navigation与XCode结合使用

mam*_*mcx 10 iphone objective-c

我正在试图组合一个TabBar +导航应用程序.

我有5个标签栏,4个列出了东西并深入查看详细信息视图.

我尝试按照本教程:

http://www.iphonedevforums.com/forum/iphone-sdk-development/124-view-controller-problem.html

但总是得到一个空白的观点.

这就是我所做的,有一个干净的项目:

  • 我从一个TabBar模板应用程序开始.
  • 我放了5个标签栏按钮.
  • 我创建一个控制器,如:

    @interface FirstViewController:UINavigationController {

    }

  • 我将主window.xib放在树模式下并将所选的第一个视图更改为 FirstViewController
  • 我在"界面"构建器中选择了TabBar控制器,转到TabBar属性并将类更改为导航控制器.
  • 选择第一个视图并将笔尖名称设为"SecondView"

作为回应,我得到一个空白屏幕.

我必须补充一点,我想从细节视图导航,而不是从主窗口导航.

即在主窗口中,标签栏1是人员列表.我选择一个人然后想导航到细节窗口.

And*_*sov 15

首先,您永远不想继承UINavigationController或UITabBarController.

其次,我并没有完全了解你所做的,但是在标签栏控制器中创建导航控制器的正确顺序是这样的:

// in MyAppDelegate.h
UINavigationController *nc1, *nc2;
FirstTabRootViewController *vc1;
SecondTabRootViewController *vc2;
UITabBarController *tbc;

// in MyAppDelegate.m
nc1 = [[UINavigationController alloc] init];
vc1 = [[FirstTabRootViewController alloc] initWithNibName:nil bundle:nil];
vc1.tabBarItem.title = @"Tab 1";
vc1.tabBarItem.image = [UIImage imageNamed:@"tab1.png"];
vc1.navigationItem.title = "Tab 1 Data";
nc1.viewControllers = [NSArray arrayWithObjects:vc1, nil];

nc2 = [[UINavigationController alloc] init];
vc2 = [[SecondTabRootViewController alloc] initWithNibName:nil bundle:nil];
vc2.tabBarItem.title = @"Tab 2";
vc2.tabBarItem.image = [UIImage imageNamed:@"tab2.png"];
vc2.navigationItem.title = "Tab 2 Data";
nc2.viewControllers = [NSArray arrayWithObjects:vc2, nil];

tbc = [[UITabBarController alloc] init];
tbc.viewControllers = [NSArray arrayWithObjects:nc1, nc2, nil];
Run Code Online (Sandbox Code Playgroud)

请注意,它是您的视图控制器,用于控制选项卡栏和导航栏中的文本/图标.为每个选项卡创建一个UINavigationController实例; UINavigationController包含一堆视图控制器(请参阅viewControllers属性),该控制器应包含至少一个项目 - 该选项卡的根控制器.还要创建一个UITabBarController来管理选项卡.

当然,您可以(也可能应该)使用接口构建器而不是代码来实例化提到的类并设置属性.但重要的是要了解幕后发生的事情; 接口构建器只不过是实例化和设置对象的便捷方式.

希望这有用; 如果不是,请改进你的问题.


ben*_*ord 1

这是我能够开始工作的教程。

我还阅读了有关该主题的官方 SDK 文档: Combining Tab Bar and Navigation Controllers。由于我仍在学习,因此教程比文档对我帮助更大。

注意:在本教程中,我认为您不需要子类化 UINavigationController,我现在正在尝试这个想法。