0

我有一个带有包含三个选项卡的 Tabbar 控制器的应用程序。我需要将导航控制器添加到第一个选项卡。我的根视图控制器是标签栏,那么如何添加导航栏?我已经初始化导航栏,但不知道在哪里设置它。感谢任何可以提供帮助并询问您是否需要更多信息的人。这是我的应用程序 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
4

1 回答 1

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

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

标签栏:[导航控制器:[VC1],VC2,VC3]

于 2012-07-08T18:24:16.740 回答