我正在开发一个应用程序,我正在使用标签栏控制器。当应用程序打开时,它应该只显示一个标签栏项目"餐厅"当我从任何一家餐馆的菜单中选择一些食品时,应该显示另一个名为项目的标签栏视图控制器。请帮我解决这个问题。
提前致谢。
答案 0 :(得分:0)
这是一种方式:
根据需要创建应用程序,并确保在添加TabBarController
时,应该只有一个标签项(删除其他项目,默认情况下在添加tabbarcontroller时添加)
创建UITabBarController
从故事板分配所有控制器的类以及分配特定的故事板标识符
声明BOOL
类型的全局属性(在用户默认值或您要维护的位置)我已添加到AppDelegate.h
@property (assign, nonatomic) BOOL isAddNew;
现在,您要在tabor中添加(显示)新标签,添加代码,我的代码为:
- (IBAction)addNewTab:(id)sender
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.isAddNew = TRUE;
[self.tabBarController viewDidLoad];
}
然后转到自定义tabbarcontroller(在步骤2中创建 - > UITabBarController
的子类),我的班级为.h
#import <UIKit/UIKit.h>
@interface TabViewController : UITabBarController
@end
.m
是
#import "TabViewController.h"
#import "AppDelegate.h"
#import "ViewController2.h"
#import "ViewController3.h"
@interface TabViewController ()
{
AppDelegate *appDelegate;
}
@end
@implementation TabViewController
- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate.isAddNew == TRUE)
{
ViewController2 *view2 = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];
//ViewController3 *view3 = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController3"];
NSMutableArray *tabViewControllers = [self.viewControllers mutableCopy]; // [[NSMutableArray alloc] init];
[tabViewControllers addObject:view2];
//[tabViewControllers addObject:view3];
[self setViewControllers:tabViewControllers];
//can't set this until after its added to the tab bar
// ------ First way ------
view2.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Second" image:[UIImage imageNamed:@"selectimg.png"] tag:1];
// ------ Second way ------
view2.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Second" image:[UIImage imageNamed:@"deselectimg"] selectedImage:[UIImage imageNamed:@"selectimg.png"]];
// ------ Third way ------
view2.tabBarItem = [[UITabBarItem alloc] init];
view2.tabBarItem.selectedImage = [[UIImage imageNamed:@"selectimg.png”]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
view2.tabBarItem.image = [[UIImage imageNamed:@"deselectimg.png”]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
view2.tabBarItem.title = @"Second";
//view3.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Third" image:[UIImage imageNamed:@"third.png"] tag:2];
[appDelegate.window setRootViewController:self];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}