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

发布于 2024-11-05 14:20:51 字数 1759 浏览 0 评论 0原文

在我的 iPhone 应用程序中,我有一个带有三个选项卡的通用选项卡栏,按下按钮后会从多个视图中显示。我遵循的方法是 Tweetie 应用程序的工作流程,如 罗伯特·康恩帖子

注意,主控制器是导航控制器;选项卡栏放置在导航堆栈的视图控制器的 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 为我指明了正确的方向。

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.

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

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 {
  ...   
}

...

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.

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

Thanks!!

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半步萧音过轻尘 2024-11-12 14:20:51

下面的代码有解决方案:

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

The following code has the solution:

NSMutableArray *tbViewControllers = [NSMutableArray arrayWithArray:[self.tabBarController viewControllers]];
[tbViewControllers removeObjectAtIndex:2];
[self.tabBarController setViewControllers:tbViewControllers];
兔小萌 2024-11-12 14:20:51

斯威夫特4

func removeTab(at index: Int) {
    guard let viewControllers = self.tabBarController?.viewControllers as? NSMutableArray else { return }
    viewControllers.removeObject(at: index)
    self.tabBarController?.viewControllers = (viewControllers as! [UIViewController])
}

Swift 4

func removeTab(at index: Int) {
    guard let viewControllers = self.tabBarController?.viewControllers as? NSMutableArray else { return }
    viewControllers.removeObject(at: index)
    self.tabBarController?.viewControllers = (viewControllers as! [UIViewController])
}
半衬遮猫 2024-11-12 14:20:51

这就是它对我来说适用于 Swift 4 和 5

  1. 创建自定义 UITabBarController 类。
  2. 将自定义 UITabBarController 类分配给故事板上的视图。
  3. 删除 viewDidLoad 上的 UIViewController

    类 TabViewController: UITabBarController {
    
        覆盖 func viewDidLoad() {
            super.viewDidLoad()
    
            self.viewControllers?.remove(at: 1)
        }
    }
    

This is what it works for me for Swift 4 and 5

  1. Create a custom UITabBarController class.
  2. Assign the custom UITabBarController class to the view on storyboard.
  3. Remove the UIViewController on viewDidLoad:

    class TabViewController: UITabBarController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.viewControllers?.remove(at: 1)
        }
    }
    
策马西风 2024-11-12 14:20:51

您可以在类中保存对该选项卡栏对象的引用,并对其执行所需的操作。

IBOutlet <Type> name;

通过界面构建​​器连接它,您可以执行操作,在您的情况下,您可能会考虑将其从超级视图中删除。

You can hold a reference to that tab bar object in your class and perform the desired actions on it.

IBOutlet <Type> name;

Connect it via Interface builder and you can perform actions, and in your case you may be thinking of removing it from superview.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文