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

Josh Benard
Josh Benard
3,919 Points

Code Challenge 2: A Storyboard with a Tab Bar Controller

Here's the challenge:

Tabs have a property named 'badgeValue' that displays text in a red circle in the upper-right corner of the tab, like the number badges on the App Store or Messages apps. The tabs (UITabBarItems) are already stored in an array named 'tabBarItems'. Access the item in the array that corresponds to the selected index and then set the badge value to '3' for that UITabBarItem. Remember that 'badgeValue' must be an NSString!

My code:

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

    [self.tabBarController setSelectedIndex:2];

    self.tabBarController.tabBar.selectedIndex badgeValue:@"3";

}

@end

I don't really understand the statements in the challenge, so I don't really know what my code should look like here. Help?

7 Answers

Stone Preston
Stone Preston
42,016 Points

sure.

So we create an array of tabBarItems. We set it to hold the items of the tab bar controllers tab bar

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

the tab bar controller has a selected index property, which sets which tab is set or selected (0 is the first tab, 1 is the second, etc. We want to set the selected index to 2, or the third tab.

[self.tabBarController setSelectedIndex:2];

We want to set the badgeValue property of the tab bar item at this selected index in the tabBarItems array, which we have just set to 2. We need access to this tab bar item first though, which is stored in our tabBarItems array

To access this item, we create a new UITabBarItem object, and assign it to the object in our tabBarItems array at the selected index of the tab bar controller

UITabBarItem *item = tabBarItems[self.tabBarController.selectedIndex];

Since the selected index is 2, this translates into

 UITabBarItem *item = tabBarItems[2];

our UITabBarItem object now points to the value of the object stored in the 2nd index of the tabBarItems array. Since we have our item now, we can modify the badge value and set it to a string.

item.badgeValue = @"3";

Thank you a lot Stone Preston!!!

Stone Preston
Stone Preston
42,016 Points

you are almost there youve set the selected index, now you just have to modify the badge value property of the tabBaritem in your array at that index

UITabBarItem *item = tabBarItems[self.tabBarController.selectedIndex];

You set the badgeValue using the badgeValue property of tabBarItems

item.badgeValue = @"whatever badge value you need";

Edit: i answered this question a little more in depth here

Josh Benard
Josh Benard
3,919 Points

Gotcha. Final question. How are you getting your code in those cool code blocks that hi-light syntax??

Stone Preston
Stone Preston
42,016 Points

you can add a blank line then indent your code by 4 spaces.

like this

there are multiple ways. this post covers them all pretty much

Josh Benard
Josh Benard
3,919 Points

That worked, but can you "sentence diagram" why exactly that works or how the process flow worked there?

Josh Benard
Josh Benard
3,919 Points

So technically...

tabBarItems[2].badgeValue=@"3";

and

tabBarItems[self.tabBarController.selectedIndex].badgeValue=@"3";

are the same thing?

Stone Preston
Stone Preston
42,016 Points

in this case yes, but thats only because the selected index is set at 2. however the selected index changes, so it wouldnt always be the same the same thing.

Stone Preston , You are a big help man. Thanks for all of your contributions to these forums. Are you employed at Treehouse or working elsewhere as an iOS developer?

Stone Preston
Stone Preston
42,016 Points

im actually just a student at the moment. No not employed by treehouse haha, just enjoy lending a hand every now and then.

stone preston gave a great answer and explanation