带导航栏的标签栏应用程序

时间:2011-11-04 12:01:04

标签: ios uinavigationcontroller uitabbarcontroller

我正在构建一个基于TabBar的应用程序。其中一个选项卡将显示一个TableView,我希望在顶部有一个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设置标题。

我真的很感激你的帮助。

3 个答案:

答案 0 :(得分:5)

您需要在UINavigationController的{​​{1}}的第二个索引处添加tabBarController,而不是添加视图控制器 -

viewControllers

要在导航栏设置标题,您可以在- (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; } 的{​​{1}} {/ p>中写下这个标题

RecycleViewController

答案 1 :(得分:0)

您可以使用Interface Builder本地化xib,因此您不必以编程方式执行此操作。

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

答案 2 :(得分:0)

xcode 4.3.2的答案是:

在RecycleViewController.m中分配UINavigationBar:

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

但是有一个错误,导航栏会在顶部显示一个空白,大约10像素。

所以这是另一个解决方案: 使用xib builder,在视图顶部的libary中添加一个导航栏RecycleViewController.xib,按ctrl +“导航项”,指向RecycleViewController.h插入一个IBOutlet,然后你会看到一个好看的导航栏。