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

iOS Self Destructing Message App - UI Programmatically

After completing the Photo Browser app (which is built completely in code - without Interface Builder), I've taken on the challenge of rebuilding the Self Destructing Message app completely in code.

Conceptually, this doesn't seem too difficult, apart from one section. Unfortunately, this is the first section!

I'm finding it difficult to understand or envisage how I could go about setting up a UITabBarController which is the rootViewController for self.window in AppDelegate.m, and at the same time embed each of the three view controllers (Inbox, Friends, and Camera) within UINavigationControllers.

I have tried to implement this myself, but I'm afraid I'm making it up as I go along and possibly developing bad habits, which I obviously don't want to do.

If someone could give me a high-level overview of how this could be achieved, I'd really appreciate it! You don't necessarily need to write any code, just an explanation of some steps would be perfect.

Thanks!

2 Answers

Nice to see someone else preferring to do stuff programmatically as well :) It should look like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    TNInboxViewController *inboxViewController = [[TNInboxViewController alloc] init];
    TNFriendsViewController *friendsViewController = [[TNFriendsViewController alloc] init];
    TNCameraViewController *cameraViewController = [[TNCameraViewController alloc] init];

    UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:inboxViewController];

    UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:friendsViewController];

    UINavigationController *navigationController3 = [[UINavigationController alloc] initWithRootViewController:cameraViewController];

    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    [tabBarController setViewControllers:@[navigationController1, navigationController2, navigationController3]];

    self.window.rootViewController = tabBarController;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

Perfect! Thanks for that.

Have an awesome day.