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 Build a Self-Destructing Message iPhone App Designing and Starting the App A Storyboard with a Tab Bar Controller

Eilon Krauthammer
Eilon Krauthammer
4,768 Points

I don't understand task 2

I don't really understand the instructions, help please? :)

3 Answers

Ben Falk
Ben Falk
3,167 Points

It's worded confusingly. I just tried it and got a successful answer, so maybe this explanation will help...

This challenge asks you to create a new UITabBarItem called tab, which should be set to the current active item in the tab bar (item number 3, also known as index 2). You should be able to use the array tabBarItems to access the items in the tab bar.

Hope that helps?

Ben Falk
Ben Falk
3,167 Points

Here is the exact question: The tabs are stored in an array named tabBarItems (line 9). Declare a UITabBarItem variable named tab and set it from tabBarItems. Use the selectedIndex we just set as the index for the tab you get from the tabBarItems array.

I'm not sure how better to explain it... Here's the correct answer for task 2:

#import "MainViewController.h"
#import "UITabBarItem.h"

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSArray *tabBarItems = self.tabBarController.tabBar.items;

    // Add your code below! The property for MainViewController's 
    // tab bar controller is named 'tabBarController'.

    // from task 1
    [self.tabBarController setSelectedIndex:2];

    // task 2
    UITabBarItem *tab = tabBarItems[self.tabBarController.selectedIndex];
}

@end

With this code, I am creating a new UITabBarItem called tab. Its value is set using the tabBarItems array created earlier in the code, which is an array of individual tab bar items. Since it's an array, I can use square brackets to access specific items within the array, so I am selecting the currently selected index from the tabBarController (which was just set in the first line), as the instructions ask.

Christopher Dyer
Christopher Dyer
3,634 Points

Shouldn't the challenges be related to the preceding video?!? Shouldn't they be simple constructs in which all elements are clear - in question and code.

Normally, when stuck, a review of the tutorial not only provides the answer, but cements the details.

The only way to correctly answer these questions is to find the answers in the forum and paste them in. I've forgotten what the tutorial was about!