2011-11-04 42 views
1

我正在构建一个基于TabBar的应用程序。其中一个选项卡将显示一个TableView,我想在顶部有一个NavigationBar。TabBar应用程序与NavigationBar

但是我需要将本应用程序中的所有内容本地化为多种语言,因此需要在代码中完成。

我有在AppDelegate中设置的标签栏;

@implementation AppDelegate 

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



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
    UIViewController *viewController1 = [[HomeViewController alloc] initWithNibName:@"HomeView" bundle:nil]; 
    UIViewController *viewController2 = [[RecycleViewController alloc] initWithNibName:@"RecycleView" bundle:nil]; 
    UIViewController *viewController3 = [[SettingsViewController alloc] initWithNibName:@"SettingsView" bundle:nil]; 
    UIViewController *viewController4 = [[SettingsViewController alloc] initWithNibName:@"SettingsView" bundle:nil]; 

    self.tabBarController = [[UITabBarController alloc] init]; 
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2,viewController3, viewController4, nil]; 
    self.window.rootViewController = self.tabBarController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

每个窗口都有该选项卡上信息的代码。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     self.title = NSLocalizedString(@"Home", @"Home"); 
     self.tabBarItem.image = [UIImage imageNamed:@"home"]; 
    } 
    return self; 
} 

到目前为止好,但RecyclingView(TableView)需要一个导航栏,我可以使用NSLocalizedString设置标题。

我会很感激一些帮助。

回答

5

您需要将您tabBarControllerviewControllers而不是添加视图控制器的第二索引处添加一个UINavigationController -

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
    UIViewController *viewController1 = [[HomeViewController alloc] initWithNibName:@"HomeView" bundle:nil]; 
    UIViewController *viewController2 = [[RecycleViewController alloc] initWithNibName:@"RecycleView" bundle:nil]; 
    UIViewController *viewController3 = [[SettingsViewController alloc] initWithNibName:@"SettingsView" bundle:nil]; 
    UIViewController *viewController4 = [[SettingsViewController alloc] initWithNibName:@"SettingsView" bundle:nil]; 

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController2]; 

    self.tabBarController = [[UITabBarController alloc] init]; 
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, navController,viewController3, viewController4, nil]; 
    self.window.rootViewController = self.tabBarController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

设置标题导航栏,你可以这样写在RecycleViewControllerviewDidLoad

[self.navigationItem setTitle:@"title"]; 
0

ŧ他回答的Xcode 4.3.2:

页头的UINavigationBar的在RecycleViewController.m就象这样:

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self]; 
self.view addSubview:navController.view; 

但是有一个bug,即导航栏将在顶部显示一个空白,约10pixels。

因此,这里是另一种解决方案: 使用厦门国际银行建设者,加libary导航栏鉴于RecycleViewController.xib,按Ctrl +“导航项目”的顶部,指向RecycleViewController.h插入一个IBOutlet,那么你将有一个很好看的导航栏来显示。

相关问题