将导航栏添加到当前程序

时间:2012-07-08 18:19:03

标签: objective-c ios cocoa-touch

我有一个包含三个标签的Tabbar控制器的应用。我需要在第一个选项卡中添加导航控制器。我的根视图控制器是标签栏,那么如何添加导航栏?我已初始化导航栏但不知道在哪里设置它。感谢任何可以提供帮助的人,并询问您是否需要更多信息。这是我的app delegate.m:

#import "TACAppDelegate.h"
#import "FirstViewController.h"
#import "ThirdViewController.h"
#import "SecondViewController.h"

@implementation TACAppDelegate

@synthesize window = _window;
@synthesize tabBarController = _tabBarController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary     *)launchOptions
{
/* Initialize window view */
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

/* Initialize tab bar controller, add tabs controllers */
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [self initializeTabBarItems];
self.window.rootViewController = self.tabBarController;

[self.window makeKeyAndVisible];
return (YES);
}

....

- (NSArray *)initializeTabBarItems
{
NSArray * retval;

/* Initialize view controllers */
FirstViewController *viewController1 = [[FirstViewController alloc] init];
SecondViewController *viewController2 = [[SecondViewController alloc] init];
ThirdViewController *viewController3 = [[ThirdViewController alloc] init];

/* Initialize navigation controllers */
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];



/* Stuff Navigation Controllers into return value */
retval = [NSArray arrayWithObjects:viewController1,viewController2,viewController3,nil];



return (retval);
}

@end

1 个答案:

答案 0 :(得分:1)

这是在代码中需要更改以执行此操作的行:

/* Stuff Navigation Controllers into return value */
retval = [NSArray arrayWithObjects:viewController1,viewController2,viewController3,nil];

在这里,您仍然将原始viewController1的引用放入标签栏项目数组中。这不再是你想要的。您已经创建了导航控制器并将其根视图控制器设置为视图控制器1,因此您需要将顶级参考放入标签栏项目列表中。你的新行看起来像这样:

/* Stuff Navigation Controllers into return value */
retval = [NSArray arrayWithObjects: navigationController1,viewController2,viewController3,nil];

这将创建一个控制器层次结构,如:

TabBar:[NavController:[VC1],VC2,VC3]