如何以编程方式删除在父类 NIB 文件中创建的选项卡栏项?

人气:838 发布:2022-10-16 标签: iphone ios4 nib uitabbar uitabbaritem

问题描述

在我的 iPhone 应用程序中,我有一个带有 三个标签 的公共标签栏,在按下按钮后会从多个视图中呈现.我遵循的方法是 Tweetie 应用程序的工作流程,在 Robert Conn 帖子.

Within my iPhone application I have a common tab bar with three tabs that is presented from several views after pressing a button. The approach I followed was the workflow of Tweetie application, described in Robert Conn post.

注意主控制器是一个导航控制器;选项卡栏放置在导航堆栈的视图控制器的 NIB 文件中,选项卡之间的切换效果在委托的 didSelectItem 方法中处理.

Note that the main controller is a navigation controller; the tab bar is placed in a view controller's NIB file of the navigation stack, and the effect of switching between tabs is handled in a delegate didSelectItem method.

@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 文件的父类.然后,我创建了几个子类:

Since the views of the tab bar must be customized according to the view controller the application came from, I have made this GameTabBarController a parent class with that NIB file that have the tab bar. Then, I have created several children classes:

@interface FirstGameTabBarController : GameTabBarController {
  ...   
}

@interface SecondGameTabBarController : GameTabBarController {
  ...   
}

...

我的问题是,在某些子类中,我想删除与父类关联的 NIB 文件的第三个选项卡.但由于没有涉及 UITabBarController,我无法遵循您可以在网络上找到的典型方法,即删除标签栏项目的视图控制器.

My problem is that in some of the children classes I would like to remove the third tab of the NIB file associated with parent class. But since there is no UITabBarController involved, I cannot follow typical approaches you can found on the web, i.e. removing the view controller of the tab bar item.

我该怎么做?是否可以删除之前在 NIB 文件中添加的元素?

How can I do that? Is it possible to remove elements that has been previously added in a NIB file?

谢谢!!

更新解决方案非常简单......我只需替换标签栏项目,而不是视图控制器:

UPDATE The solution was so easy... I have just to replace the tab bar items, instead of the view controllers:

NSMutableArray *items = [NSMutableArray arrayWithArray:self.tabBar.items];
[items removeObjectAtIndex:2];
[self.tabBar setItems:items];

感谢@Praveen S 为我指明了正确的方向.

Thanks to @Praveen S for pointing me in the right direction.

推荐答案

以下代码有解决方案:

NSMutableArray *tbViewControllers = [NSMutableArray arrayWithArray:[self.tabBarController viewControllers]];
[tbViewControllers removeObjectAtIndex:2];
[self.tabBarController setViewControllers:tbViewControllers];

713