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

code challenge: tab bar view controller

hey, so I'm on sections 1/2 for the code challenge and it says :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. so here my code
#import "MainViewController.h"

import "UITabBarItem.h"

@implementation MainViewController

  • (void)viewDidLoad { [super viewDidLoad];

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

    self.tabBarController.selectedIndex:2; [[tabBarItems selectedIndex:2] badgeValue:[NSString setBadgeValue:@"3"];

}

@end

and i cant get it to work. i read another post and didnt get it but answer and reason would be great.

5 Answers

I was struggling with this too for a while and finally figured it out.

For me, I conceptualized it like this. We need to call a method on an object. The code for that generally looks like this.

[(insert object here) (followed by method)];

We know that the object is: self.tabBarController (this is the object where we want to select the third tab from) We know that the method is setSelectedIndex: We know that to assign it the third tab we go with 2

So the above yields

[self.tabBarController setSelectedIndex:2];

sorry im on part 2 and cant get it

I feel as though I'm missing a few key pieces of information to make this work. Can someone help me on this one?

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

How did you know that? Was is in the videos or were you on developer.apple website snooping around till you found it?

never mind got it

Hello! I am stuck on this too... is there a clue that you can help me up with?