UITabBarControllerの使い方
HooコントローラとFooコントローラを持つUITabBarへ移動
-(IBAction)HogeAndFooPressed:(id)sender { //---------------------------------------------------------------------------// // HogeViewControllerとFooViewController生成 //---------------------------------------------------------------------------// HogeViewController *hogeViewController = [[HogeViewController alloc] init]; FooViewController *fooViewController = [[FooViewController alloc] init]; //---------------------------------------------------------------------------// // タブバー生成を生成してHogeViewControllerとFooViewControllerを登録 //---------------------------------------------------------------------------// UITabBarController *tabController = [[UITabBarController alloc] init]; NSArray* controllers = [NSArray arrayWithObjects: hogeViewController, fooViewController, nil ]; tabController.viewControllers = controllers; //---------------------------------------------------------------------------// // navigationControllerにタブバーをpush //---------------------------------------------------------------------------// [self.navigationController pushViewController:tabController animated:YES]; // リリース [tabController release]; }
説明
navigationControllerにpushするのはHogeViewControllerとFooViewControllerを持ったタブバー
[self.navigationController pushViewController:tabController animated:YES];
各自のタブをHogeViewController, FooViewControllerが自分自身で作る。
HogeViewController.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { UIImage* anImage = [UIImage imageNamed:@"Hoge.png"]; UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"Hoge" image:anImage tag:0]; self.tabBarItem = theItem; [theItem release]; } return self; }
FooViewController.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { UIImage* anImage = [UIImage imageNamed:@"Foo.png"]; UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"Foo" image:anImage tag:0]; self.tabBarItem = theItem; [theItem release]; } return self; }