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

A Storyboard with a Tab Bar Controller

For this code challenge:

We just saw how to change tabs by tapping on them, but we can also do this programmatically using the 'selectedIndex' property of UITabBarController. The tab bar controller in the example below has three tabs. Set the selected tab as the third tab using either dot-notation or the 'setSelectedIndex:' method. Note: The tabs are indexed just like arrays, meaning the first is at index zero.

I understand the concept but I don't know how to start. I tried looking up the documentation but there are a few pieces of information about the syntax that I just don't know. Can anyone help with this?

I really found this one unnecessarily confusing and not at all covered in the video. There is an NSArray presented that has nothing to do with this part of the challenge and at this point we have no concept of referencing the UITabBarController. Hate when I have to break a good flow to Google something like this and skim through posts.

7 Answers

Thanks! I also realized that I was forgetting the brackets.

[self.tabBarController setSelectedIndex:2];

? This guy == super n00b

I had a similar question and found this thread helpful. Not a noob, just trying to learn.

Glad it helped. To clarify I was talking about myself. I am totally new to all of this.

At first I cringed hard when I saw this comment.. Then I saw the name and realized you were criticizing yourself. ; p

Well, you have two contexual clues here. You've got a tab bar controller, which has a property for the selected index/tab, which can be set by accessing the property directly or by sending a message with setSelectedIndex. You've also got a set of tabs indexed like an array.

Can you clarify what exactly you're confused about? I think I could give you the answer, but that wouldn't help as much as trying to point you in the right direction.

Am I missing the whole part about setting it based on the array? I was thinking that because

#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'.

}

@end

NSArray *tabBarItems was created that I could just setSelectedIndex

You're pretty close. But tabBarItems is a collection/array of the items on the bar, which have properties like title, image, etc. They don't determine which one is selected. You want to tell the controller which one is selected.

And yeah, I can see how NSArray *tabBarItems being there would add some confusion into thinking that you're supposed to do something with it.

Happy I could help!

My guess was to use:

self.tabBarItems setSelectedIndex:2;

but I'm missing something.

I was also trying

self.tabBarItems.selectedIndex = 2;

also to no avail.