views:

82

answers:

1

Is there a difference between these two lines of code?

self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:1];

and

self.tabBarController.selectedIndex = 1;

My app just crashed on the top statement so it might have some memory issues. Are there any advantages of one method over the other?

A: 

Actually, setting the selectedIndex property will do something similar as in your first statement, but it will probably do some more checking. There will be a difference when there are more than 5 controllers in your tabBar, so when there is a 'More' tab. This is what the documentation tells us:

- selectedIndex

This property nominally represents an index into the array of the viewControllers property. However, if the selected view controller is currently the More navigation controller, this property contains the value NSNotFound. Setting this property changes the selected view controller to the one at the designated index in the viewControllers array. To select the More navigation controller itself, you must change the value of the selectedViewController property instead.

In versions of iPhone OS prior to version 3.0, this property reflects the index of the selected tab bar item only. Attempting to set this value to an index of a view controller that is not visible in the tab bar, but is instead managed by the More navigation controller, has no effect.

JoostK
Interesting. Thanks.
Bryan