带有NavigationBar的TabBar应用程序

Jor*_*gen 1 uitabbarcontroller uinavigationcontroller ios

我正在构建一个基于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;
}
Run Code Online (Sandbox Code Playgroud)

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

- (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;
}
Run Code Online (Sandbox Code Playgroud)

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

我真的很感激一些帮助.

saa*_*nib 5

你需要添加UINavigationController你的第二个指数tabBarControllerviewControllers,而不是添加视图控制器-

- (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;
}
Run Code Online (Sandbox Code Playgroud)

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

[self.navigationItem setTitle:@"title"];
Run Code Online (Sandbox Code Playgroud)