2011-11-04 86 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,那麼你將有一個很好看的導航欄來顯示。

相關問題