如何更改 UITabBarController 的默认 ViewController [英] How to change default ViewController of UITabBarController

查看:33
本文介绍了如何更改 UITabBarController 的默认 ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 2 个 ViewControllers 创建了基本的 TabBarController.它有效,但它有错误,我想要一些关于它的东西.

I create basic TabBarController with 2 ViewControllers. It worked but it have bug and i want something about it.

问题 1.

当我运行时(默认ViewController 是FirstViewController)TabBarSecondViewController 的名称没有出现.

When I run (default ViewController is FirstViewController) name of SecondViewController in TabBar is not appear.

我想关注这张照片.

问题 2.在我的代码中,默认视图是 FirstViewController.如果我想设置默认视图是 SecondViewControllerby Same Tab Bar(FirstView Tab : Left , and SecondView Tab : Right)跟着这张图.如何解决.

Problem2. In my code default view is FirstViewController. If I want to set default view is SecondViewController by Same Tab Bar(FirstView Tab : Left ,and SecondView Tab : Right) following this picture. How to resolve it.

这是我的示例代码

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

    FirstViewController *firstVC = [[FirstViewController alloc] init];
    UINavigationController *firstNVC = [[UINavigationController alloc] initWithRootViewController:firstVC];
    firstNVC.navigationBar.barStyle = UIBarStyleBlack;

    SecondViewController *secondVC = [[SecondViewController alloc] init];
    UINavigationController *secondNVC = [[UINavigationController alloc] initWithRootViewController:secondVC];
    secondNVC.navigationBar.barStyle = UIBarStyleBlack;

    UITabBarController *tabController = [[UITabBarController alloc]init];
    tabController.navigationItem.hidesBackButton = NO;
    tabController.viewControllers = [NSArray arrayWithObjects:firstNVC, secondNVC, nil];
    tabController.navigationController.navigationBarHidden = NO;

    self.window.rootViewController = tabController;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

谢谢.^^

推荐答案

UIViewController *viewController1 = [[[viewController1 alloc] initWithNibName:@"viewController1" bundle:nil] autorelease];
    UIViewController *viewController2 = [[[viewController2 alloc] initWithNibName:@"viewController2" bundle:nil] autorelease];
    UIViewController *viewController3 = [[[viewController3 alloc] initWithNibName:@"viewController3" bundle:nil] autorelease];


    navControl1=[[UINavigationController alloc]initWithRootViewController:viewController1];
    navControl2=[[UINavigationController alloc]initWithRootViewController:viewController2];
    navControl3=[[UINavigationController alloc]initWithRootViewController:viewController3];

    navControl1.navigationBar.tintColor=[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
    navControl2.navigationBar.tintColor=[UIColor blackColor];
    navControl3.navigationBar.tintColor=[UIColor blackColor];

    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    self.tabBarController.delegate=self;
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:navControl2,navControl1,navControl3, nil ];

    [[[[self.tabBarController tabBar] items] objectAtIndex:2] setFinishedSelectedImage:[UIImage imageNamed:@""] withFinishedUnselectedImage:[UIImage imageNamed:@""]];
    [[[[self.tabBarController tabBar] items] objectAtIndex:0] setTitle:@"1"];
    [[[[self.tabBarController tabBar] items] objectAtIndex:0] setImage:[UIImage imageNamed:@"tab2.png"]];
    [[[[self.tabBarController tabBar] items] objectAtIndex:1] setTitle:@"2"];
    [[[[self.tabBarController tabBar] items] objectAtIndex:1] setImage:[UIImage imageNamed:@"tab11.png"]];
    [[[[self.tabBarController tabBar] items] objectAtIndex:2] setTitle:@"3"];
    [[[[self.tabBarController tabBar] items] objectAtIndex:2] setImage:[UIImage imageNamed:@"tab5.png"]];

    [self.tabBarController.tabBar setSelectionIndicatorImage:[UIImage imageNamed:@"trans.png"]];

    UIImage* tabBarBackground = [UIImage imageNamed:@""];
    [[UITabBar appearance] setBackgroundImage:tabBarBackground];
    [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"trans.png"]];

这篇关于如何更改 UITabBarController 的默认 ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆