views:

58

answers:

2

UIViewController *newView = [[UIViewController alloc] initWithNibName:@"NewView" bundle:[NSBundle mainBundle]];

tabBarController.selectedViewController = newView;

Why doesn't this work?

A: 

The selectedViewController property of a UITabBarController must be one of the tab bar's existing tabs (as defined in its viewControllers property). You may want to either push the new controller onto an existing tab (which would have to be a UINavigationController), or add the viewController to the viewControllers array.

Ben Gottlieb
I've made the current tab bar with IB, not programatically, int numControllers = [tabBarController.viewControllers count]; returns 0 - I'm guessing this has something to do with it
DaveyDivDovs
A: 

Because that view controller isn't part of the UITabBarController.

If you look at the Apple reference for the selectedViewController property, you'd see:

The specified view controller must be in the viewControllers array.

You need to add the view controller to UITabBarController, using the viewControllers property, so the view controller has a tab, so you can then select that view controller's tab.

Shaggy Frog