Graphics Programs Reference
In-Depth Information
UITabBarController
View controllers become more interesting when the user's actions can cause another view
controller to be presented. In this topic, we will show you a number of ways to present
view controllers. We'll start with a UITabBarController to swap between instances
of HypnosisViewController and TimeViewController .
UITabBarController keeps an array of UIViewController s. It also maintains a
tab bar at the bottom of the screen with a tab for each view controller in this array. Tapping
on a tab results in the presentation of the view of the view controller associated with that
tab.
In HypnoAppDelegate.m , create an instance of UITabBarController , give it both
view controllers, and install it as the rootViewController of the window.
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
HypnosisViewController *hvc = [[HypnosisViewController alloc] init];
TimeViewController *tvc = [[TimeViewController alloc] init];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
NSArray *viewControllers = [NSArray arrayWithObjects:hvc, tvc, nil];
[tabBarController setViewControllers:viewControllers];
[[self window] setRootViewController:tvc];
[[self window] setRootViewController:tabBarController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Build and run the application. Tap on the black tabs at the bottom of the screen to swap
between the two view controllers.
Notice that the UITabBarController is the rootViewController of the window.
This means that UITabBarController is itself a subclass of UIViewController .
A UITabBarController , then, has a view property. Its view is a blank UIView
with two subviews: the tab bar and the view of the selected view controller ( Figure 7.13 ).
Figure 7.13 UITabBarController diagram
 
 
Search WWH ::




Custom Search