如何在ios7中显示所有标签栏标题

时间:2014-06-14 04:22:36

标签: ios objective-c uiviewcontroller uitabbarcontroller

基本上,当我运行我的应用程序时,我无法显示所有tabBar项目,只显示第一个视图控制器:

enter image description here

我必须单击选项卡才能显示其项目:

enter image description here

这是我在Appdelegate.m中的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Initialize window
    self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];

    // Set background colors for both NavBar and TabBar
    [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:0.157 green:0.718 blue:0.553 alpha:1]];
    [[UITabBar appearance] setBarTintColor:[UIColor colorWithRed:0.141 green:0.216 blue:0.263 alpha:1]];

    // Initialize your five tab controllers.  with each tab has its own navigation controller
    HomePageView *homePageView = [[HomePageView alloc]init];
    UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:homePageView];

    ProfileViewController *profileViewController=[[ProfileViewController alloc]init];
    UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:profileViewController];

    FeedViewController *feedViewController=[[FeedViewController alloc]init];
    UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:feedViewController];

    ListeningSessionViewController *listeningSessionViewController= [[ListeningSessionViewController alloc]init];
    UINavigationController *nav4 = [[UINavigationController alloc]initWithRootViewController:listeningSessionViewController];

    RecievedViewController *recievedViewController =[[RecievedViewController alloc]init];
    UINavigationController *nav5 = [[UINavigationController alloc]initWithRootViewController:recievedViewController];

    // initialize tabbarcontroller,set your viewcontrollers and change its color.
    self.tabC = [[UITabBarController alloc]init];
    NSArray* controllers = [NSArray arrayWithObjects: nav1,nav2,nav3,nav4,nav5, nil];
    [self.tabC setViewControllers: controllers animated:NO];
    [_window addSubview:self.tabC.view];

    // Show window
    [self.window makeKeyAndVisible];


    return YES;
}

1 个答案:

答案 0 :(得分:1)

我猜你是在控制器的viewDidLoad或viewDidAppear方法中设置标题。这是行不通的,因为虽然所有控制器都在app委托中实例化,但只有索引0的控制器加载了它的视图,因此不会为其他控制器运行viewDidLoad。相反,您应该在应用程序委托中的导航控制器上设置标题,

ProfileViewController *profileViewController=[[ProfileViewController alloc]init];
UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:profileViewController];
nav2.tabBarItem.title = @"Profile";