UITabBarControllelrとは
こんなやつです.
iPodはタブが5つあるものが実装されていますよね!!
今回はこれについて紹介します.
UITabBarControllerは複数のUIViewControllerを管理します.
上の例では,2つのUIViewControllerを管理しています.
これを実装するのは,ちょっと難しいのでさっそくコードを書いてみましょう!!
先にコードを全部のせますねー.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
// UITabBarControllerのインスタンスを作ります
UITabBarController *tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
// UITabBarControllerに管理させるUIViewControllerを作成します.
// 今回は2つのUIViewControllerを管理してみましょう
UIViewController *controller1 = [[UIViewController alloc] initWithNibName:nil bundle:nil];
controller1.view.backgroundColor = [UIColor redColor];
// それぞれ分かりやすいように背景に色をつけてみましょう
UIViewController *controller2 = [[UIViewController alloc] initWithNibName:nil bundle:nil];
[controller2.view setBackgroundColor:[UIColor greenColor]];
// UITabBarControllerに設定するための前準備です
NSArray *controllers = [NSArray arrayWithObjects:controller1, controller2, nil];
// UIViewControllerを設定します
[tabBarController setViewControllers:controllers];
// UITabBarControllerのタブのタイトルを設定しています
NSArray *items = tabBarController.tabBar.items;
[[items objectAtIndex:0] setTitle:@"view1"];
[[items objectAtIndex:1] setTitle:@"view2"];
// 起動後にはじめに表示するViewControllerに設定します
self.window.rootViewController = tabBarController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
今回は少し長くなってしまいましたが,ノンビリ解説していくことにします.
次の記事へ
HOMEに戻る