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

我已经成功编写了这个代码标签栏,但我的问题是它会直接在第一个标签中显示第二个窗口我想在第一个标签上看到第一个视图我该怎么做提前谢谢..

4

3 回答 3

0

我认为您正在尝试为ViewController第一个和最后一个选项卡提供相同的实例。可能这就是问题所在。再次为ViewController类创建另一个实例并将该类添加到数组中。

于 2013-09-12T09:04:47.703 回答
0

您的问题是您已添加ViewController * vc, vc 两次。

你的代码:

[array addObject:vc];

[array addObject:hvc];

[array addObject:svc];

[array addObject:vc]; <-这是错误的。

=========================

于 2013-09-12T09:05:38.017 回答
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];
于 2013-09-12T09:06:27.563 回答