今回はナビゲーションについてまとめます。
↓こんな感じのやつです。
iPhoneでゲームとか作ってます



・UIViewControllerについて

UIViewControllerは、直接の描画オブジェクトであるUIViewにいろいろ付加機能をつけて
ラッピングしている感じのクラス。
iPhoneアプリの標準的な画面遷移はUIViewControllerを基本にしているので、
そういうのを使う場合は、必須になってくる。
今回のナビゲーションもこのクラスが基本。


・UINavigationControllerを作成
ナビゲーションをしたいときはUINavigationControllerを使います。
UINavigationControllerUIViewControllerのサブクラスなので、
専用のviewをもっていて、これをwindowにaddSubviewすると
外枠にナビゲーションがつきます。
ではさっそく、ナビゲーションを作ってしまいましょう!って思うのですが、
ナビゲーションには、最初に中に設定するメインのビューを決めてあげないと
いけないので、まず、メインのビューを定義をして、それからナビゲーションを設定します。

ここで、クラス変数を以下の設定がされてるものとします。


UINavigationController* navi; // ナビゲーション
UIViewController* uv1; // メインビュー
UIViewController* uv2; // 次のビュー




uv1 = [[UIViewController alloc] init]; // メインビューを作成
navi = [[UINavigationController alloc] initWithRootViewController:uv1]; // ナビを作成
[window addSubview:navi.view]; // メインウインドウにadd



これでナビが表示されます。



・ビューの切り替え
ビューを切り替えるには、


[navi pushViewController:uv2 animated:TRUE];


を使います。
これで、ビューを切り替えつつ、ビューを階層的に保持するので、戻りボタンは勝手に出てきます。
なので、これ1つで、次のビューに切り替えつつ、元に戻る仕組みも備わってしまいます。
animated=TRUEのときはにゅい~んって感じ、FALSEのときはスパっと切り替わります。

メインのウインドウにボタンを置いて押したときに切り替えのコマンドが起動するように
しておけば、ボタンを押して画面が切り替わって、ナビ左上のボタンを押すと元に
戻る仕組みが完成です。



・タイトルとかナビ内のボタンとか
ナビゲーションに表示されるタイトルは、中に表示するUIViewControllerに設定します。

uv1.title = @"View1";
uv2.title = @"View2";



とすることで、ビューの切り替えでタイトルも変わってくれます。

そのほか、ナビ内のボタンとか、フッタを表示したときにその中に置くボタンとかも
中に表示されるUIViewControllerが設定を保持するようになっています。

ボタンの設定はUIBarButtonItemで定義します。


UIBarButtonItem* itm = [[UIBarButtonItem alloc] initWithTitle:@"tool1" style:UIBarButtonItemStyleBordered target:self action:@selector(tool1On:)];


スタイルは

UIBarButtonItemStylePlain(標準)
UIBarButtonItemStyleBordered(普通のボタン)
UIBarButtonItemStyleDone(水色のボタン)

target, actionで、ボタンが押された時の呼び出しオブジェクト、メソッドを指定します。

ナビの左右に置く場合は


uv2.navigationitem.leftBarButtonItem = itm; // 左におくとき
uv2.navigationitem.rightBarButtonItem = itm; // 右におくとき



左に置いた場合は、戻るボタンを置き換えてしまうので、別の方法で戻る仕組みを作らないと戻れなくなりそうです。

フッタに表示するときは、


uv2.toolBarItems = [NSArray arrayWithObject:itm];


フッタはデフォルトで非表示になっているので、


navi.toolBarHidden = FALSE;


を設定して表示状態にする必要があります。


・盛り込むとこんな感じ
部分的ですが。。。

[ビューの生成とか]

uv1 = [[UIViewController alloc] init];
uv1.title = @"View1";
UIBarButtonItem* itm = [[UIBarButtonItem alloc] initWithTitle:@"tool1" style:UIBarButtonItemStyleDone target:self action:@selector(tool1On:)];
uv1.toolbarItems = [NSArray arrayWithObject:itm];
UIButton* btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn1 setTitle:@"next" forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(btn1On:) forControlEvents:UIControlEventTouchUpInside];
btn1.frame = CGRectMake(60, 130, 200, 100);
[uv1.view addSubview:btn1];
navi2 = [[UINavigationController alloc] initWithRootViewController:uv1];
navi2.toolbarHidden = FALSE;
[window addSubview:navi2.view];

uv2 = [[UIViewController alloc] init];
uv2.title = @"View2";
UIBarButtonItem* itm2 = [[UIBarButtonItem alloc] initWithTitle:@"tool2" style:UIBarButtonItemStylePlain target:self action:@selector(tool2On:)];
uv2.navigationItem.rightBarButtonItem = itm2;




[イベント発生時の起動メソッド]

- (void)btn1On:(id)btn{
[navi2 pushViewController:uv2 animated:TRUE];

}
- (void)tool1On:(id)btn{
UIAlertView* v = [[UIAlertView alloc] initWithTitle:@"alert" message:@"tool1On!" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:nil];
[v show];

}
- (void)tool2On:(id)btn{
UIAlertView* v = [[UIAlertView alloc] initWithTitle:@"alert" message:@"tool2On!" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:nil];
[v show];

}

注意説明優先なので、メモリリークは無視しています。


以上です。

・・・こんな感じかなぁ??