如何以编程方式删除在父类 NIB 文件中创建的选项卡栏项目?
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
下面的代码有解决方案:
The following code has the solution:
斯威夫特4
Swift 4
这就是它对我来说适用于 Swift 4 和 5
UITabBarController
类。UITabBarController
类分配给故事板上的视图。删除
viewDidLoad
上的UIViewController
:This is what it works for me for Swift 4 and 5
UITabBarController
class.UITabBarController
class to the view on storyboard.Remove the
UIViewController
onviewDidLoad
:您可以在类中保存对该选项卡栏对象的引用,并对其执行所需的操作。
通过界面构建器连接它,您可以执行操作,在您的情况下,您可能会考虑将其从超级视图中删除。
You can hold a reference to that tab bar object in your class and perform the desired actions on it.
Connect it via Interface builder and you can perform actions, and in your case you may be thinking of removing it from superview.