UINavigationControllerの利用
Interface Builderを利用せずにナビゲーションメニューを実装します。今回はソースコードをアップしたので主要な箇所のみ載せてます。
まずはapplicationDidFinishLaunchingでUINavigationControllerを生成、スタイルを設定し、起動時に表示するUIViewControllerを追加します。
sample01AppDelegate.m
- (void)applicationDidFinishLaunching:(UIApplication *)application { MyViewController *mvc = [[[MyViewController alloc] initWithNibName:nil bundle:nil] autorelease]; naviController = [[UINavigationController alloc] init]; //UIControllerViewを追加 [naviController pushViewController:mvc animated:NO]; //バーを消す //[naviController setNavigationBarHidden:YES animated:NO]; //バーのスタイルを指定 //[naviController.navigationBar setBarStyle:UIBarStyleBlackOpaque]; //バーの色を指定 //naviController.navigationBar.tintColor = [UIColor grayColor]; [window addSubview:naviController.view]; [window makeKeyAndVisible]; }
UIViewControllerのinitWithNibNameで設定したタイトルがナビゲーションバーに表示されます。UIパーツを追加する場合はviewDidLoadで。ここでは次ページに遷移するためのボタンを追加しています。ページ遷移させるのはpushViewControllerでUIViewControllerを指定するだけなので楽チンです。
MyViewController.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { self.title = @"PAGE1"; } return self; } - (void)viewDidLoad { [super viewDidLoad]; //バーにボタンを追加 UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"次ページ" style:UIBarButtonItemStyleBordered target:self action:@selector(goNextPage:) ]; self.navigationItem.rightBarButtonItem = button; [button release]; } -(void)goNextPage:(id)sender{ MyViewController2 *mvc2 = [[[MyViewController2 alloc] initWithNibName:nil bundle:nil] autorelease]; [self.navigationController pushViewController:mvc2 animated:YES]; }