Jorgen
Jorgen

Reputation: 475

TabBar application with NavigationBar

I'm building an App which is TabBar based. One of the tabs will display a TableView and I would like to have a NavigationBar at the top.

However I need to localize everything in this app to several languages so it needs to be done in code.

I have the tab bar set up in the 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;
}

Each of the windows has the code for the information on the tab.

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

So far so good, but the RecyclingView (TableView) need a navigation bar where I can set the title using NSLocalizedString.

I would be really grateful for some help.

Upvotes: 1

Views: 4014

Answers (3)

ashora
ashora

Reputation: 7

The answer for xcode 4.3.2 is:

alloc the UINavigationBar in RecycleViewController.m as this:

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

But there is a bug, that navigation bar will display a blank at the top, about 10pixels.

So here is another solution: use xib builder, add a navigation bar from libary in the top of view RecycleViewController.xib, press ctrl+"navigation Item", point to RecycleViewController.h to insert an IBOutlet,then you will have a good looking navigation bar to show.

Upvotes: 0

saadnib
saadnib

Reputation: 11145

you need to add a UINavigationController at the second index of your tabBarController's viewControllers instead of adding a view controller -

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

to set the title at navigation bar you can write this in your RecycleViewController's viewDidLoad

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

Upvotes: 5

Randall
Randall

Reputation: 14839

You can localize the xibs with Interface Builder so you shouldn't have to do it programatically.

http://www.icanlocalize.com/site/tutorials/iphone-applications-localization-guide/

Upvotes: 0

Related Questions