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


Need help on a code challenge using the selectedIndex property of UITabBarController

The question 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. Use dot-notation or the setSelectedIndex: method to set the 3rd tab as the selected tab. Note: The tabs are indexed just like arrays, meaning the 1st tab is at index 0.

I have tried both dot-notation and the setSelectedIndex methods but I keep getting it wrong.... I just don't know what else to do! Please help!!!

4 Answers

Patrick Serrano
Patrick Serrano
13,834 Points

This would be a passing answer:

[self.tabBarController setSelectedIndex:2];

Based on the answers you tried it looks like you're still a little confused about how to call methods on objects and so I would suggest taking anther look at the videos to help give you a better understanding.

A basic overview is that inside the square brackets the object you're calling the method on comes first, followed by a space and then the method you want to call. So in this case the self.tabBarController is the object and setSelectedIndex: is the method. (The 2 is the parameter that the method takes.) Hopefully this helps.

Patrick Serrano
Patrick Serrano
13,834 Points

Can you post all of your code so we can take a look?

Sure! I tried these already:

[[tabBarItems tabBarController] setSelectedIndex:2];

Also this one:


And this one:

tabBarController.tabBarItems = 2;

And this:

[tabBarItems setSelectedItem:2];

Thank you for your help.... I have been revisiting those videos already, but you explanation really helped me put it together. Thanks again!!!