2010-12-01 101 views
2

我希望每个人都可以explane我如何做到这一点:加入UITabBarItems到UITabBar

我有一个的TabBar和两个TabBarItems,我怎么能attatch该项目的TabBar。 我不是通过IB来做这件事,因为TabBar只适用于屏幕,因为项目应该在左侧。

这就是我如何建立他们:

tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil]; 
tabBarController2 = [[UITabBarController alloc] initWithNibName:nil bundle:nil]; 

tabBarController.tabBar.frame = CGRectMake(0, 974, 384, 50);  
tabBarController2.tabBar.frame = CGRectMake(384, 974, 384, 50); 

UITabBarItem *tbi1 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:0]; 
UITabBarItem *tbi2 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:1]; 

回答

6

不要直接在标签栏设置标签栏项目。而是将标签栏项目分配给标签栏控制器包含的每个视图控制器的tabBarItem属性。然后,当您将视图控制器添加到标签栏控制器时,标签栏控制器将管理您的标签栏项目的显示。

UITabBarController * tabBarController = [[UITabBarController alloc] init]; 

UIViewController * viewController1 = [[YourViewController alloc] init]; 
UIViewController * viewController2 = [[YourOtherViewController alloc] init]; 

viewController1.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:0]; 
viewController2.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:1]; 

tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil]; 
+0

我已经试过,但它没有为我工作,但这是我的第二个TabBar造成的。 – Frank 2010-12-01 18:04:49