Reputation: 2666
I have an app that have several views. In one view I would like to add a UITabBarController
.
In another app I used this to add the UITabBarController
to the rootViewController
, but I am not sure if that is even the correct way to do it.
in the .h
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
in the .m
@synthesize tabBarController=_tabBarController;
self.window.rootViewController = self.tabBarController;
Upvotes: 1
Views: 4576
Reputation: 12780
In one of my app i have used this code to set tabbarcontroller in between app
define tabbar controller in AppDelegate.m
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate=self;
self.tabBarController.selectedIndex=0;
self.tabBarController.delegate=self;
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
//NSUInteger index=[[tabBarController viewControllers] indexOfObject:viewController];
// return YES;
}
Apply below code where you want to push your controller with tab bar controller
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
UIViewController *viewController1 = [[GeneralViewController alloc] initWithNibName:@"GeneralViewController" bundle:nil];
UIViewController *viewController2 = [[MiscQuotationController alloc] initWithNibName:@"MiscQuotationController" bundle:nil];
UIViewController *viewController4 = [[QuotationListController alloc] initWithNibName:@"QuotationListController" bundle:nil];
UIViewController *viewController5 = [[ChargesViewController alloc] initWithNibName:@"ChargesViewController" bundle:nil];
UIViewController *viewController7 = [[SalesPartViewController alloc] initWithNibName:@"SalesPartViewController" bundle:nil];
/// tab button title
viewController1.title = @"Basic information";
viewController2.title = @"Misc Quotation";
viewController4.title = @"Quotation Line";
viewController5.title = @"Charges";
viewController7.title = @"Sales Part Stock";
// tab button Images
viewController1.tabBarItem.image = [UIImage imageNamed:@"general.png"];
viewController2.tabBarItem.image = [UIImage imageNamed:@"misle.png"];
viewController4.tabBarItem.image = [UIImage imageNamed:@"history.png"];
viewController5.tabBarItem.image = [UIImage imageNamed:@"charges.png"];
viewController7.tabBarItem.image = [UIImage imageNamed:@"shoebox.png"];
delegate.tabBarController = [[UITabBarController alloc] init];
delegate.tabBarController.selectedIndex = 0;
delegate.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, viewController4, viewController5, viewController7, nil];
delegate.tabBarController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.navigationController pushViewController:delegate.tabBarController animated:YES];
Upvotes: 3
Reputation: 5098
Instead of writing this
self.window.rootViewController = self.tabBarController;
Write this in the code:
[self.window addSubview:self.tabBarController.view];
You cannot add UITabbarController
to UIViewController
, but you can add UITabBar
to UIViewController's
view.
Hope this might help you.......
Upvotes: 0