iOS 7 - TabBarController没有文本标签

时间:2014-02-17 03:53:05

标签: ios uitabbarcontroller

我正在尝试使用UITabBarController创建一个应用程序。我已经在线跟踪了几个教程,似乎我错过了一些东西,因为我的UITabBarController没有文字,但你可以从一个视图切换到另一个视图。

这是我的didFinishWithLaunchingOptions功能:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    self.tabController = [[UITabBarController alloc] init];

    UIViewController *viewController1 = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
    ListViewController *listVC = [[ListViewController alloc] initWithStyle:UITableViewStylePlain];

    self.tabController.viewControllers = [[NSArray alloc] initWithObjects:viewController1, listVC, nil];

    self.window.rootViewController = self.tabController;

    [self.window makeKeyAndVisible];
    [self createCopyOfDatabaseIfNeeded];

    return YES;
}

有谁可以指出我错过了什么?

非常感谢任何帮助,谢谢。

2 个答案:

答案 0 :(得分:3)

您需要设置每个视图控制器的标签栏图像和标题:

self.tabController = [[UITabBarController alloc] init];

UIViewController *viewController1 = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
ListViewController *listVC = [[ListViewController alloc] initWithStyle:UITableViewStylePlain];

viewController1.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"VC1" image:[UIImage imageNamed:@"tab_ yourViewController.png"] tag:0];
listVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"listVC" image:[UIImage imageNamed:@"tab_listvc.png"] tag:1];

self.tabController.viewControllers = [[NSArray alloc] initWithObjects:viewController1, listVC, nil];

viewDidLoad中为每个视图控制器设置tabBarItem也是可能的(通常是首选的),即:

- (void)viewDidLoad
{
    [super viewDidLoad]; // Be a good citizen!
    self.tabBarItem = [[UITabBarItem alloc] init...];
    ...

答案 1 :(得分:1)

您可以使用代码

为每个视图设置tabbar项目
UITabBar *tabBar = self. tabController.tabBar;
UITabBarItem *tabBarItem0 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:1];

[tabBarItem0 setTitle:@"title1"];
[tabBarItem0 setImage:[UIImage imageNamed:@"img1"]];
[tabBarItem1 setTitle:@"title2"];
[tabBarItem1 setImage:[UIImage imageNamed:@"img2"]];

self.window.rootViewController = self.tabController;

[self.window makeKeyAndVisible];