Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a Tab Bar Controller Programmatically to current App Flow

I want to add a tab Bar Controller to my current App Flow. Currently I have a page with a button which on clicking opens a new viewcontroller with a web view where the user logs in and after login I want to take him to his home Page where the navigation bar has his name and a logout button in the right. The home page should also have a tab bar with 3 different tabs. I am able to load the home page view from the webview and get the navigation bar. But I am unable to add the tabBar and get it working. I am confused as to where to add the code for adding TabBar. I am using the below code to add tab bar -

UITabBarController *tabBar = [[UITabBarController alloc] init];

HomeViewController *home = [[PPHomeViewController alloc] initWithUserName:[self.userInfo objectForKey:@"name"] Email:[self.userInfo objectForKey:@"email"] Phone:[self.userInfo objectForKey:@"phone_number"]];
home.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];
UINavigationController *homeNavController = [[UINavigationController alloc]initWithRootViewController:home];

RequestViewController *req = [[RequestMoneyViewController alloc]init];
req.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:2];
UINavigationController *reqNavController = [[UINavigationController alloc]initWithRootViewController:req];

UIViewController *thirdViewController = [[UIViewController alloc]init];
thirdViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:3];
UINavigationController *thirdNavController = [[UINavigationController alloc]initWithRootViewController:thirdViewController];

UIViewController *fourthViewController = [[UIViewController alloc]init];
thirdViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:3];
UINavigationController *fourthNavController = [[UINavigationController alloc]initWithRootViewController:fourthViewController];

tabBar.viewControllers = [[NSArray alloc] initWithObjects:homeNavController, reqNavController, thirdNavController, fourthNavController, nil];
tabBar.delegate=self;
tabBar.selectedIndex=0;

UIImageView *homeImg=[[UIImageView alloc]initWithFrame:CGRectMake(0, 432, 80, 49)];
homeImg.tag=11;
homeImg.image=[UIImage imageNamed:@"footer"];

UIImageView *reqImg=[[UIImageView alloc]initWithFrame:CGRectMake(81, 432,80, 49)];
reqImg.tag=12;
reqImg.image=[UIImage imageNamed:@"footer"];

UIImageView *sendImg=[[UIImageView alloc]initWithFrame:CGRectMake(162, 432,80, 49)];
sendImg.tag=13;
sendImg.image=[UIImage imageNamed:@"footer"];

UIImageView *localImg=[[UIImageView alloc]initWithFrame:CGRectMake(243, 432, 80, 49)];
localImg.tag=14;
localImg.image=[UIImage imageNamed:@"footer"];

[tabBar.view addSubview:homeImg];
[tabBar.view addSubview:reqImg];
[tabBar.view addSubview:sendImg];
[tabBar.view addSubview:localImg];

[[[UIApplication sharedApplication]keyWindow]addSubview:tabBar.view];

Currently I have put the above code in the viewDidLoad of a ViewController TabViewController which extends UITabBarController. In my webView controller I have put the following code -

TabViewController *tab=[[TabViewController alloc] init];
tab.userInfo=userInfo;
[self presentViewController:tab animated:YES completion:nil];

But the app crashes as soon as I click any tab other than the one already open. Please Help.

like image 891
Akshay Goel Avatar asked May 06 '13 10:05

Akshay Goel


People also ask

How do I add a tab bar to my navigation controller?

To add a tab, first drag a new View Controller object to the storybard. Next control-drag from the tab bar controller to new view controller and select view controllers under Relationship Segue . Your tab bar controller will update with a new tab.

What is the use of tab bar controller in Xcode?

The tab bar interface displays tabs at the bottom of the window for selecting between the different modes and for displaying the views for that mode. This class is generally used as-is, but may also be subclassed. Each tab of a tab bar controller interface is associated with a custom view controller.

What is tab bar controller in Swift?

What's A Tab Bar Controller? A tab bar controller, of class UITabBarController, is a container view controller. It typically organizes 3-5 view controllers in a group. The user of your app can switch between view controllers by tapping one of the tabs in the tab bar at the bottom of the screen.


1 Answers

The way I've done this in the past is to create a UITabBarController subclass that contains all of the tabBar creation code you have above.

Then use your UINavigationController to push the tabBar subclass to the screen.

Here's a sample of my UITabBarController subclass:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIViewController *view1 = [[UIViewController alloc] init];
    UIViewController *view2 = [[UIViewController alloc] init];
    UIViewController *view3 = [[UIViewController alloc] init];

    NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
    [tabViewControllers addObject:view1];
    [tabViewControllers addObject:view2];
    [tabViewControllers addObject:view3];

    [self setViewControllers:tabViewControllers];
    //can't set this until after its added to the tab bar
    view1.tabBarItem = 
      [[UITabBarItem alloc] initWithTitle:@"view1" 
                                    image:[UIImage imageNamed:@"view1"] 
                                      tag:1];
    view2.tabBarItem = 
      [[UITabBarItem alloc] initWithTitle:@"view2" 
                                    image:[UIImage imageNamed:@"view3"] 
                                      tag:2];
    view3.tabBarItem = 
      [[UITabBarItem alloc] initWithTitle:@"view3" 
                                    image:[UIImage imageNamed:@"view3"] 
                                      tag:3];      
}
like image 169
andrew lattis Avatar answered Oct 04 '22 04:10

andrew lattis