tabbar=[[UITabBarController alloc]init];
NSMutableArray *array = [[NSMutableArray alloc] init];
ViewController * vc=[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
tabbar.title=@"Home";
HomeViewController *hvc= [[HomeViewController alloc]initWithNibName:@"HomeViewController" bundle:nil];
hvc.tabBarItem.title=@"Home";
hvc.tabBarItem.image=[UIImage imageNamed:@"home.png"];
SettingViewController* svc =[[SettingViewController alloc]initWithNibName:@"SettingViewController" bundle:nil];
svc.tabBarItem.title=@"My Hospital";
svc.tabBarItem.image=[UIImage imageNamed:@"myhospital.png"];
[array addObject:hvc];
[array addObject:svc];
[array addObject:vc];
tabbar.viewControllers = array;
tabbar.view.autoresizingMask=UIViewAutoresizingFlexibleBottomMargin;
[self.view addSubview:tabbar.view];
我写的这个代码标签栏已成功添加,但我的问题是它将直接显示第一个标签中的第二个窗口我希望第一个标签上的第一个视图我怎么可以提前感谢...
答案 0 :(得分:0)
我认为您正在尝试将同一个ViewController
实例提供给第一个和最后一个标签。可能是那个问题。再次为ViewController
类创建另一个实例,并将该类添加到数组。
答案 1 :(得分:0)
您的问题是您已添加ViewController * vc
,vc两次。
[array addObject:vc];
[array addObject:hvc];
[array addObject:svc];
[array addObject:vc];
< - 这是错误的。
========================
答案 2 :(得分:0)
试试这个:
tabbar = [[UITabBarController alloc] init];
ViewController *vc = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
HomeViewController *hvc = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
hvc.tabBarItem.title = @"Home";
hvc.tabBarItem.image = [UIImage imageNamed:@"home.png"];
SettingViewController* svc = [[SettingViewController alloc]initWithNibName:@"SettingViewController" bundle:nil];
ViewController *vc2 = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
svc.tabBarItem.title = @"My Hospital";
svc.tabBarItem.image = [UIImage imageNamed:@"myhospital.png"];
tabbar.viewControllers = [NSArray arrayWithObjects: vc, hvc, svc, vc2, nil];
tabbar.view.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
tabbar.selectedIndex = 0;
[self.view addSubview:tabbar.view];