Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Extra Credit - Add UITabBarController programatically

I'm working on the extra credit for the Build a Self-Destructing app where we are trying to create a UITabBarController programatically. I've done some quick searching around on the internet and haven't been able to come up with a solution, so I'm hoping someone can help me here. Here's my code below:

import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

  • (void)viewDidLoad { [super viewDidLoad]; UITabBarController *barController = [[UITabBarController alloc] init];

    UIViewController *controllerOne = [[UIViewController alloc]init]; UIViewController *controllerTwo = [[UIViewController alloc]init]; UIViewController *controllerThree = [[UIViewController alloc]init]; UIViewController *controllerFour = [[UIViewController alloc]init]; UIViewController *controllerFive = [[UIViewController alloc]init];

    NSArray *tabs = [NSArray arrayWithObjects:controllerOne,controllerTwo,controllerThree,controllerFour,controllerFive, nil]; barController.viewControllers = tabs;

I've been working on this problem for the last couple of days with no luck. I searched through dozens of SO posts and found nothing that really helps. Everything I've found so far has directed me to declaring a new root view controller in the app delegate implementation file but all the examples I've seen make use of storyboards.

This is my code below, taken from the 'viewDidLoad' method' of my UIViewController implementation file - it's very similar to yours:

UIViewController *fvc = [[UIViewController alloc] initWithNibName:@"1" bundle:nil];
UIViewController *svc = [[UIViewController alloc] initWithNibName:@"2" bundle:nil];
UIViewController *tvc = [[UIViewController alloc] initWithNibName:@"3" bundle:nil];
UIViewController *fvc2 = [[UIViewController alloc] initWithNibName:@"4" bundle:nil];
UIViewController *fvc3 = [[UIViewController alloc] initWithNibName:@"5" bundle:nil];

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

myTabBar.viewControllers = [NSArray arrayWithObjects:fvc, svc, tvc, fvc2, fvc3, nil];

The unfortunate news is that I found this same issue addressed in the Treehouse forums from a year ago with no answers. I hope our teacher, Ben Jakuben, might read this and be able to provide some insight.

1 Answer

I'm not certain, but perhaps you need to set your tabs after they are initialized.

- (void)viewDidLoad
{
    [super viewDidLoad];
    UITabBarItem *happyTab = [[UITabBarItem alloc] initWithTitle:@"My Cool Tab" image:(UIImage *) tag:1;
    [self setTabBarItem:happyTab];
}

Where (UIImage *) is an image you have available. Instead of using a tag you could use setSelectedImage if you prefer. Start typing init(WithTitle) and see what autocompletes to your liking.