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
Dan Boyle
2,978 PointsSecond Code Challenge In Ribbit Project
I am having some issues completing this code challenge. (This is going to be a long project... in a good way though!)
Here's the part I can't seem to figure out.
**** Directions of challenge **** The tabs are stored in an array named tabBarItems (line 9). Declare a UITabBarItem variable named tab and set it from tabBarItems. Use the selectedIndex we just set as the index for the tab you get from the tabBarItems array. **** Directions of challenge end ****
**** My code so far ****
-
(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];
UITabBarItem *tab = [[UITabBarItem alloc] initWithTabBarSystemItem:???];
}
**** My code so far end ****
I read about UITabBarItem and it seems it needs to be allocated and initiallized with initWithTabBarSystemItem in this case?
I feel that I am also making this harder than it needs to be.
Here's what the knowledge base gave me...
- (id)initWithTabBarSystemItem:(UITabBarSystemItem)systemItem tag:(NSInteger)tag
But I don't really understand that... even when I keep reading more.
2 Answers
Stone Preston
42,016 Pointsyou dont need to allocate and init it. just set it using the tabBarItems array and the selected index as the index of the array
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];
UITabBarItem *tab = [tabBarItems objectAtIndex:self.tabBarController.selectedIndex];
Dan Boyle
2,978 PointsThanks Stone!
Why don't you have to allocate and initialize UITabBarItem?
Stone Preston
42,016 Pointsbecause the tab bar item already exists in memory (you dont need to create a new one). you are just creating another variable to point to it so you can reference it.
Dan Boyle
2,978 PointsDan Boyle
2,978 PointsThanks Stone!
Why don't you have to allocate and initialize UITabBarItem?