我们如何为IOS中的所有视图实现通用标签栏?

时间:2013-01-17 04:21:20

标签: ios objective-c

我是IOS的新手。在我的应用程序的家中,有3个选项卡和4个按钮(使用导航控制器)。我在appdelegate.m中创建了一个tabbarcontroller,如下所示:

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.


//Initializing the view controllers
UIViewController *disController1 = [[DisController1 alloc]initWithNibName:@"DisController1" bundle:nil];
UIViewController *viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
UIViewController *wishlistController = [[Wishlist alloc]initWithNibName:@"Wishlist" bundle:nil];
UIViewController *helpController = [[HelpController alloc]initWithNibName:@"HelpController" bundle:nil];
UIViewController *disController2 = [[DisController1 alloc]initWithNibName:@"DisController2" bundle:nil];


NSMutableArray *viewControllersArray = [[NSMutableArray alloc]init];
[viewControllersArray addObject:disController1];
[viewControllersArray addObject:viewController];
[viewControllersArray addObject:wishlistController];
[viewControllersArray addObject:helpController];
[viewControllersArray addObject:disController2];



//Initializing the tab bar controller
UIImage *img = [UIImage imageNamed:@"SES_tab bar"];

CGSize newSize = CGSizeMake(SCRN_WIDTH, TABHEIGHT);

Common *common = [[Common alloc]init];
UIImage *newImage = [common imageResize:img andResizeTo:newSize];
[common setTabBarColorandImage:newImage];

tabController = [[UITabBarController alloc]init];


tabController.viewControllers = [NSArray arrayWithObjects:disController1, viewController,wishlistController,helpController,disController2 ,nil];

tabController.selectedIndex = 1;
UITabBarItem *firstTab = [[tabController.tabBar items]objectAtIndex:0];
UITabBarItem *lastTab = [[tabController.tabBar items]objectAtIndex:4];
[firstTab setEnabled:NO];
[lastTab setEnabled:NO];


UINavigationController *navCtrl = [[UINavigationController alloc]initWithRootViewController:tabController];
//navCtrl.navigationBarHidden = NO;

//[navCtrl.navigationBar setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];
[navCtrl.navigationBar setBarStyle:UIBarStyleBlackTranslucent ];
[navCtrl.navigationBar.topItem setTitle:@"Home"];
[self.window addSubview:tabController.view];


self.window.rootViewController = navCtrl;
[self.window makeKeyAndVisible];
return YES;

TabBar适用于我的主页。但我的问题是当我点击一个按钮时,标签栏不会出现在新视图中。我该如何实现呢?

1 个答案:

答案 0 :(得分:1)

您应该为每个标签创建UINavigationControllers。将当前阵列中的每个视图控制器设置为每个导航控制器的根视图控制器。

编辑:

UIViewController *disController1 = [[DisController1 alloc]initWithNibName:@"DisController1" bundle:nil];
UIViewController *viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
UIViewController *wishlistController = [[Wishlist alloc]initWithNibName:@"Wishlist" bundle:nil];
UIViewController *helpController = [[HelpController alloc]initWithNibName:@"HelpController" bundle:nil];
UIViewController *disController2 = [[DisController1 alloc]initWithNibName:@"DisController2" bundle:nil];


UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:disController1];

//etc. for each of the view controllers that you have allocated. 

NSMutableArray *viewControllersArray = [[NSMutableArray alloc]init];
[viewControllersArray addObject:navController1];
[viewControllersArray addObject:navController2];
[viewControllersArray addObject:navController3];
[viewControllersArray addObject:navController4];
[viewControllersArray addObject:navController5];