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

Troy Fine
Troy Fine
7,592 Points

Setting Badge Value to Tab Bar Controller Item

Code Challenge 2 of 2: I've been back and forth through Xcode Documentation regarding the tab bar controller, badge value, and tab bar item.

I've attempts using both dot notation and passing objects. I've looked into setBadgeValue, badgeValue, selectedItem, selectedIndex, and more for "tabBarItems" but no matter what I try it doesn't work.

I've also tried setting the selected item for the tabBarItems first then setting the value to the badgeValue second and then thirdly passing the badgeValue to tabBarItems.

I've treated badgeValue as a NSString because badgeField is a text but I think the confusion might be in weather badgeValue needed to be created or is already set like it is in Xcode, according to the Xcode documentation as a property of UITabBarController.

I did both just to be sure but I'm still unsure why I can't get the badgeValue to be assigned to the tabBarItem I want it to.

I would give sample code but I've tried so many things that any sample code would be irrelevant to the many other attempts I've made. Believe me, my first 12 attempts were legit and logical. The other 24 were left to guesswork and wishful yet creative thinking.

If someone could point out my flawed approach or point me in the right direction, much appreciation would be theirs.

2 Answers

Matthew Mascioni
Matthew Mascioni
20,444 Points

Hi Troy,

Sorry to hear you're having trouble with the challenge! I remember it giving me a lot of difficulty when I did it, too. You were right about the badgeValue property being of type NSString, as the challenge states.

In the challenge, we can see that there already exists an array of UITabBarItems, called tabBarItems. Our tabBarController has a property, selectedIndex, which is of type NSUInteger.

If we look at the documentation for NSArray, we can see the instance method objectAtIndex takes in an NSUInteger as a parameter. Thus, we can declare a UITabBarItem, and point it at the UITabBarItem we currently have selected through selectedIndex.

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

UITabBarItem has a property called badgeValue, as you've seen. This is of type NSString. We can assign the value as such through dot notation:

item.badgeValue = @"3";

Hope this helps! Have fun with the rest of the project :)

Troy Fine
Troy Fine
7,592 Points

Thank you, that was very helpful, *facepalms self for not digging further in NSArray.