【问题标题】:TabBar application with NavigationBar带有 NavigationBar 的 TabBar 应用程序
【发布时间】:2011-11-04 12:01:04
【问题描述】:

我正在构建一个基于 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 设置标题。

如果能得到一些帮助,我将不胜感激。

【问题讨论】:

    标签: ios uinavigationcontroller uitabbarcontroller


    【解决方案1】:

    您需要在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"];
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        xcode 4.3.2 的答案是:

        在 RecycleViewController.m 中分配 UINavigationBar:

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

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

        所以这是另一个解决方案: 使用xib builder,在视图RecycleViewController.xib顶部从libary添加一个导航栏,按ctrl+"navigation Item",指向RecycleViewController.h插入一个IBOutlet,你就会有一个好看的导航栏显示出来。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-01-14
          • 2019-01-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-16
          • 2011-07-14
          • 1970-01-01
          相关资源
          最近更新 更多