ios - 带有 NavigationBar 的 TabBar 应用程序

标签 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 设置标题。

我会非常感谢一些帮助。

最佳答案

您需要添加 UINavigationController在您的 tabBarController 的第二个索引处的viewControllers而不是添加 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;
}

要在导航栏设置标题,您可以在 RecycleViewController 中写入的viewDidLoad
[self.navigationItem setTitle:@"title"];

关于ios - 带有 NavigationBar 的 TabBar 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8008897/

相关文章:

ios - 在 UIViewController 上动态创建 UIWebView

ios - 由嵌入在 UITabBarController 中的 UINavigationController 呈现的 View 不会调整 View 大小以考虑 View 底部的 tabBar

iphone - 更改 iPhone 上选项卡栏的颜色

ios - Swift - UITabBarController 作为 ChildViewController 不工作

ios - dateString 在 ipad 上与模拟器上显示不同

ios - 在第二个 SIAlertView 消息和 popViewController 之后屏幕锁定

ios - UINavigationController清除背景色

iphone - 标签栏 Controller 消失

ios - 在 TableViewController 和 ViewController 之间进行委托(delegate)

ios - UITextField 编辑和添加到数组