manujmv
manujmv

Reputation: 6445

How can we implement a common tabbar for all views in IOS?

I am new to IOS. In my app's home, there are 3 tabs and 4 buttons(using navigation controllers). I created a tabbarcontroller inside appdelegate.m as follows:

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 works fine for my home page.But my problem is when i clicks on a button, Tab bar doesn't appear in the new view. How can I implement that?

Upvotes: 0

Views: 457

Answers (1)

user1078170
user1078170

Reputation:

You should create UINavigationControllers for each of your tabs. Set each view controller in your current array as the root view controller of each of those navigation controllers.

EDIT:

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

Upvotes: 1

Related Questions