Adding tabs to UITabBarController without any view controllers?

Discussion in 'iOS Programming' started by scanf, Oct 7, 2011.

  1. scanf macrumors newbie

    Joined:
    Apr 23, 2010
    #1
    I'm trying to do something that is probably a bit unorthodox, but I have a UITabBarController that I want to add 4 tabs to, but all i want to do is capture the tab bar controller's selectedIndex inside of didSelectViewController and perform any needed actions on my own.

    I don't need to hide or show any views, just load different urls into a webview i already have loaded. the problem is that i don't know how to add these tabs other than to set the viewControllers property. Is there any way to accomplish what I want?

    Btw, I'm creating the UITabBarController programmatically
     
  2. scanf thread starter macrumors newbie

    Joined:
    Apr 23, 2010
    #2
    i have it kind of working thanks to this tutorial:
    http://howtomakeiphoneapps.com/how-can-i-add-tabs-programmatically-to-uitabbar/201/

    however, the UITabBarController seems to be acting modal as I now can't interact with the webview. here's my code. notice anything wrong?

    Code:
    	//set up main view controller
    	mainViewController = [[MainViewController alloc] init];	
    	[window addSubview:mainViewController.view];	
    	
    	
    	//set up the tab bar controller
    	NSMutableArray *listOfViewControllers = [[NSMutableArray alloc] init];
    	UIViewController *vc;
    	
    	vc = [[UIViewController alloc] init];
    	vc.title = @"A";
    	[listOfViewControllers addObject:vc];
    	[vc release];
    	vc = [[UIViewController alloc] init];
    	vc.title = @"B";
    	[listOfViewControllers addObject:vc];
    	[vc release];
    	vc = [[UIViewController alloc] init];
    	vc.title = @"C";
    	[listOfViewControllers addObject:vc];
    	[vc release];	
    	
    	window.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    	tabBarController = [[UITabBarController alloc] init];
        tabBarController.view.autoresizingMask =  UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        tabBarController.viewControllers = listOfViewControllers;
        [window addSubview:tabBarController.view];
        tabBarController.delegate = self;	
    
    is it because i'm adding both the mainviewcontroller and the tabbarcontroller as subviews to the window?
     
  3. scanf thread starter macrumors newbie

    Joined:
    Apr 23, 2010
    #3
    ok, the trick is to NOT use a UITabBarController, but instead a UITabBar.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    I diagree. I would use a UITabBarController and eliminate the MainViewController.
     

Share This Page