在我的 iPhone 应用程序中,我有一个带有三个选项卡的公共选项卡栏,在按下按钮后会从多个视图中显示。我遵循的方法是 Tweetie 应用程序的工作流程,在Robert Conn 的帖子中进行了描述。
注意主控制器是导航控制器;选项卡栏放置在导航堆栈的视图控制器的 NIB 文件中,选项卡之间的切换效果在委托的 didSelectItem 方法中处理。
@interface GameTabBarController : UIViewController<UITabBarDelegate> {
UITabBar *tabBar;
UITabBarItem *lastGameTabBarItem;
UITabBarItem *previousGamesTabBarItem;
UITabBarItem *myBetsTabBarItem;
NSArray *viewControllers;
UIViewController *currentViewController;
}
@implementation GameTabBarController
...
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
UIViewController *viewController = nil;
// Get the view controller linked to the tab bar item pressed
...
// Switch to the view
[self.currentViewController.view removeFromSuperview];
[self.view addSubview:viewController.view];
self.currentViewController = viewController;
}
...
@end
由于标签栏的视图必须根据应用程序来自的视图控制器进行自定义,因此我已将其GameTabBarController
作为具有标签栏的 NIB 文件的父类。然后,我创建了几个子类:
@interface FirstGameTabBarController : GameTabBarController {
...
}
@interface SecondGameTabBarController : GameTabBarController {
...
}
...
我的问题是,在某些子类中,我想删除与父类关联的 NIB 文件的第三个选项卡。但由于没有涉及 UITabBarController,我无法遵循您可以在网络上找到的典型方法,即删除标签栏项目的视图控制器。
我怎样才能做到这一点?是否可以删除以前添加到 NIB 文件中的元素?
谢谢!!
更新 解决方案非常简单......我只需替换标签栏项目,而不是视图控制器:
NSMutableArray *items = [NSMutableArray arrayWithArray:self.tabBar.items];
[items removeObjectAtIndex:2];
[self.tabBar setItems:items];
感谢@Praveen S 为我指明了正确的方向。