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

Thomas Skjelstad
Thomas Skjelstad
11,541 Points

Code Challenge: A StoryBoard with a TabBarController - badgeValue STUCK.

I am stuck on the Code Challenge below. Help anyone? I posted my code at the bottom.

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!

    #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'.
    self.tabBarController.selectedIndex = 2;
    self.tabBarItem.selectedIndex.badgeValue = @"3";
    }

    @end

23 Answers

So after trying to solve this challenge for a while, I came up with four possible solutions. They are all the same but figuring them out has really helped me understand the challenge and the code a lot better so I wanted to share it in case it might help someone else that was really stuck like me on this one.

    // 1st solution

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

    // 2nd solution

   [[tabBarItems objectAtIndex:2] 
            setBadgeValue:[NSString stringWithFormat:@"3"]];

    // 3rd solution

    UITabBarItem *newItem = [tabBarItems objectAtIndex:2];
    [newItem setBadgeValue:@"3"];

    // 4th solution - cleanest, shortest, and simplest.

    [[tabBarItems objectAtIndex:2] setBadgeValue:@"3"];

I hope that helps,

Mike

PS: If you found the above helpful, be sure to check out my app which breaks code challenges down and really explains things so you can learn faster.

Code! Learn how to program with Swift

https://itunes.apple.com/app/code!-learn-how-to-program/id1032546737?mt=8

PPS: I could not have developed the above app without going through the awesome resources on Team Treehouse. Stick with it. It is so worth it!

I'm pretty annoyed with this challenge. It's a challenge to even follow the question. If it's not going to be covered in the vid it should at least be broken up into multiple steps. I can code a tab bar controller but I can't seem to figure out what the challenge is exactly looking for.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Hi Caleb,

Sorry to hear that! I can understand -- I have some doubts about this as well. On the one hand, in an Advanced project like this, I think it's good to work through a problem that you haven't explicitly been shown yet because it's very indicative of real-world experience. However, I am not totally happy with how it is worded and it sounds like it's causing more frustration than I predicted. I will revisit it and try to improve it, possibly by breaking it up into smaller steps as you've mentioned. Let us know in here if you're still having trouble with it.

I agree. If it doesn't explicitly support the course work why include it? Figuring it out takes too much time for too little reward.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

It's included because it relates to the project but didn't fit in as a separate video. We've talked about ways we can effectively teach during the Code Challenges, too. But it's good to hear the student feedback about it so we can tweak our approach. :)

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Hi Thomas,

So with your last line there:

self.tabBarItem.selectedIndex.badgeValue = @"3";

This first part: `self.tabBarItem.selectedIndex gives you an int. Adding .badgeValue to an int gives you the Compiler Error.

Instead, let's take a look at the instructions.

The tabs (UITabBarItems) are already stored in an array named 'tabBarItems'.

So there's a hint: start with tabBarItems. :)

Access the item in the array that corresponds to the selected index

tabBarItems is an NSArray, so get the item in the array using an appropriate method and the same selected index.

and then set the badge value to '3' for that UITabBarItem.

So here, you can either create a UITabBarItem variable and set it there or just chain everything together.

Remember that 'badgeValue' must be an NSString!

Looks like you're already using the correct value there!

Thomas Skjelstad
Thomas Skjelstad
11,541 Points

I must be too tired to think clearly. I can't figure it out right now. I would have to wait until tomorrow before i sit down and look at this some more. I am sure there is a ridiculously simple solution to my problem. :) Thank you for the hint Ben. I am sure i will get good use out of it tomorrow. Norway is signing off for the day :)

Ben Jakuben
Ben Jakuben
Treehouse Teacher

I could see that - I didn't want to give away the whole answer too easily. :) Goodnight, Norway! We'll be here tomorrow to help if you need it!

Thanks for the hints, This question had me stumped for a while.

Thanks! Your hint was super helpful without just giving away the answer!

These challenge questions have nothing to do with what was discussed in the video. In the video we learned about creating tabs in storyboard not creating them programmatically. Having taught for more then a decade I would recommend rewriting this challenge to align with what was taught in the video. Testing on material not covered is Redonkulous!

Thomas Skjelstad
Thomas Skjelstad
11,541 Points

setValueBadge NSString stringWithFormat

is also used in the code.

Why is it stringWithFormat? Just trying to understand the rationale of using it since no formatting is actually passed. Are there alternatives that could be used?

//First [self.tabBarController setSelectedIndex:2];

//Second
UITabBarItem *tab = [tabBarItems objectAtIndex:2];

//Third
[tab setBadgeValue:@"3"];

So that's what I came up with, but its still not working!

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

self.tabBarController.selectedIndex = 2;

[tabBarItems objectAtIndex:2].badgeValue = @"3"; }

@end

I would be glad if anyone could help :) Is it possible to chain it like that?

Thomas Skjelstad
Thomas Skjelstad
11,541 Points

Hi Fabio,

Just started to look at the Code again. I am looking through the further reading part. Will give you a clue once i find it out or maybe you will figure it out before me :)

The way you wrote the code does not look like it would be a valid code. Not sure if you can do a dot notation after the braced code.

Hi Thomas,

ok thank you. I'm also looking around if I find something to fix it :D

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Sorry this one is causing you all trouble! I might have to revisit it. I wanted it to be challenging, and it's nice to see good discussion going on in here.

Since we don't have compile-time errors in our code challenge engine yet, one way to troubleshoot is to copy your code into Xcode to see if you can get any errors. Here's what happens if I use the code from above:

Xcode error

The reason this happens is because 'objectAtIndex' returns a generic object of type id. As such, we don't know what kind of messages we can pass to it (or more specifically, the compiler doesn't).

Though there are different ways to do it, the way I intended this challenge is to do one piece at a time. So first, get the UITabBarItem and store it in its own variable. Then on a new line access the badgeValue property (or send the setBadgeValue message. Hope this helps!

Ben Jakuben This is a great idea with copying the code into XCode. Did help me!

Btw: This was a challenging one :)

Thomas Skjelstad
Thomas Skjelstad
11,541 Points

I knew i just needed some sleep. I figured it out.

Do you want to look some more or do you want a hint?

i hint wouldn't hurt, thanks

Thomas Skjelstad
Thomas Skjelstad
11,541 Points

[[something in here] something:[something something:@"something"]];

thanks

no i already had the part you gave me but not the last part.

heres my code i dont know why its not working. [[tabBarItems objectAtIndex:2] setBadgeValue:[NSString stringWithFormat:@"3"];

thats what i had the whole time

Thomas Skjelstad
Thomas Skjelstad
11,541 Points

missing an ] should be @"3"]]; you have to close them all.

wow all that work just to realize i missed that. thanks for all the help. best of luck, Kai

I'm stuck in this challenge right now!

//I was trying this
[self.tabBarController setSelectedIndex:2];

// i'm saving the item at index 2    
UITabBarItem *item = [[self tabBarItems] objectAtIndex:2];

// and then setting de badge
[item setBadgeValue:@"3"];
Ben Jakuben
Ben Jakuben
Treehouse Teacher

So close! tabBarItems is a local variable, so you don't need to use 'self' to reference it. We use self to reference properties of the class. Remove 'self' (and one set of square brackets) and you should be set.

Why is this not working for me? I always get an error that the code engine is not responding is it due to my code?

 [self.tabBarController setSelectedIndex:2];
    UITabBarItem *item = [tabBarItems objectAtIndex:2];
    [item setBadgeValue:@“3]; 
Ben Jakuben
Ben Jakuben
Treehouse Teacher

Can you give us the exact wording of the error? Although it looks like your double quotes are different than regular ones for some reason (maybe pasted in?). That could be causing a problem.

Thanks for letting me know of the weird quotes. I did not notice that before but will pay more attention. The error that I got was very cryptic. It was a browser pop-up error in Google Chrome that said:

There was a problem connecting to the Code Challenge Engine: Timeout Error. If this problem persists, please contact support at help@teamtreehouse.com.

Also, just to confirm. The error was caused due to the funny quotes.

I believe I've tried all of the above answers and still seem to be missing something. Any tips? Here's my code:

[self.tabBarController setSelectedIndex: 2]; UITabBarItem *item = [[tabBarItems objectAtIndex:2] setBadgeValue = @"3"];

Ben Jakuben
Ben Jakuben
Treehouse Teacher

So the danger of combining method calls like this is that you can end up trying to return the wrong value. setBadgeValue is a void method, meaning it doesn't return anything. But you are trying to assign the result into a UITabBarItem variable. So in this case you need to break the statements apart: first store the result of objectAtIndex and then separately call setBadgeValue.

I was dealing with this for a while then I realized that I was misunderstanding the original question. Despite the question is kinda confusing it just takes a little bit of time to completly understand.

I feel i am cheating more with questions like this by jumping into the Forums for the answers. I can only beat my head against the wall for so long before I get annoyed with the questions or the lack of covering the subject before a quiz. Nerd Rage sets in and Google-Fu takes over.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

It's a balance! We try to avoid direct answers here in the Forum, but they definitely pop up. It's still useful, though, as long as you understand why your answer was wrong and the other answer is correct. Please do ask any follow-up questions to clear things up!

.......setBadgeValue:@"3"]]; will work.

Thomas Skjelstad
Thomas Skjelstad
11,541 Points

Just let me know if you need a better hint. Play around with it for a while.

hey do you have a better hint im also stuck.

Thomas Skjelstad
Thomas Skjelstad
11,541 Points

[[tabBarItems objectAtIndex:2] something:[something something:@"3"]];

This is as much as i can let myself to give away. You should be able to figure it out from here.

Elaine - the code you have above (a few comments ago) worked for me, as long as you fix the square bracket at the end. Worked for me!

Hope this helps!

Let's see this way:) something.something=@"3";