替代UITabBarController的子类

看来UITabBarController不应该被分类。 你会如何build议我在可旋转的DetailView中实现TabBarController?

谢谢!

您可以添加到您的控制器委托给<UITabBarDelegate>

以编程方式创build一个TabBar

UITabBar * aTabBar;

并用UITabBarItems填充它,然后实现这个function来处理一个标签上的触摸来切换视图

 - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { } 

这是代码的简要部分

 @interface yourTabsViewController : UIViewController <UITabBarDelegate> { UITabBar * mTabBar; NSMutableDictionary * mControllerPerTab; } @end 

在你的实现中:

 - (void)viewDidLoad { mControllerPerTab = [[NSMutableDictionary alloc] init]; [mControllerPerTab setValue:controller forKey:@"aKey"]; UIImage *bImage = /*icon of tab*/; UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"title" image:bImage tag:/*a tag for your tab*/]; [tabBarItems addObject:item]; } mTabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 49/*tabbbar lenght*/ - 44/*navigationbar length if it exists*/, self.view.bounds.size.width ,49)]; [mTabBar setItems:tabBarItems]; mTabBar.delegate = self; mTabBar.selectedItem = [tabBarItems objectAtIndex:0]; [self tabBar:mTabBar didSelectItem:[tabBarItems objectAtIndex:0]]; // Finally, add the tab controller view to the parent view [self.view addSubview:mTabBar]; [super viewDidLoad]; } 

然后你添加这个方法来处理标签的切换

 - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { int tag = [item tag]; /*I'm using the tag to identify wich coltroller to open*/ UIViewController * controller = [mControllerPerTab objectForKey:[NSString stringWithFormat:@"%d", tag]]; controller.view.frame = CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height - 49); [self.view addSubview:controller.view]; [self.view addSubview:mTabBar]; [self.view autoresizesSubviews]; }