问题
I create basic TabBarController
with 2 ViewControllers
.
It worked but it have bug and i want something about it.
Problem1.
When I run (default ViewController is FirstViewController
) name of SecondViewController
in TabBar
is not appear.

I want following this picture.

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.

This is my example code
- (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;
}
Thank You. ^^
回答1:
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"]];
回答2:
Problem2. If I want to set default view is SecondViewController by Same Tab Bar(FirstView Tab : Left ,and SecondView Tab : Right). How to resolve it.
Solution:
[self.tabBarController setSelectedIndex:1];
回答3:
Problem 1:
try to set a title for the view controllers.
Problem 2:
You just set selected index of the tabBarController to 1, that should do it.
回答4:
Problem1
You haven't set tabbarItem
for each navigation controller, I think you should set up a UITabBarItem
with title first, then assign it to tabbarItem
property to navigation controller.
Problem2
Just as @Kumar says.
来源:https://stackoverflow.com/questions/20966536/how-to-change-default-viewcontroller-of-uitabbarcontroller