如何更改UITabBarController的默认ViewController

时间:2014-01-07 07:52:10

标签: ios objective-c uitabbarcontroller

我使用2 TabBarController创建基本ViewControllers。 它有效,但它有bug,我想要它的一些东西。

问题1。

当我运行时(默认ViewController is FirstViewControllerSecondViewController中的TabBar名称未显示。

enter image description here

我想要关注这张照片。

enter image description here

Problem2。 的 在我的代码中,默认视图为FirstViewController。 如果我想设置默认视图是SecondViewController 通过相同标签栏(FirstView标签:左侧和SecondView标签:右侧) 跟着这张照片。如何解决它。

enter image description here

这是我的示例代码

- (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;
}

谢谢。 ^^

4 个答案:

答案 0 :(得分:0)

  

问题2. 如果我愿意的话   设置默认视图是Same Tab Bar的SecondViewController(FirstView Tab   :左,和SecondView选项卡:右)。如何   解决它。

<强>解决方案:

[self.tabBarController setSelectedIndex:1];

答案 1 :(得分:0)

问题1:

尝试为视图控制器设置标题。

问题2:

您只需将tabBarController的选定索引设置为1即可。

答案 2 :(得分:0)

<强>问题1

您尚未为每个导航控制器设置tabbarItem,我认为您应首先设置带有标题的UITabBarItem,然后将其分配给导航控制器tabbarItem属性。

<强> Problem2

正如@Kumar所说。

答案 3 :(得分:0)

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"]];