モーダルビューを使ってみる
iOSには、いくつかナビゲーションがあります
- Navigation
- Tab Bar
- モーダルビュー
のようです(iPadにはさらに他にもあります)。
Navigation
連絡先アプリなどで、項目を選ぶと、右側に画面遷移
戻るときは、画面上部の左側に出たアイコン(?)で画面遷移
Tab Bar
iPodアプリなどで、画面下に出ている5つのアイコンで、画面切り替え
ちなみに、5つ以上作るな、と
モーダルビュー
画面上部の右側に「+」とか、出ていて、タップすると、画面が下からせり上がる画面遷移。なにか追加するときなどに使われることが多い(と思う)
3つを組み合わせて作る
iPhoneのアプリは、だいたいこの3つの組み合わせで画面遷移を作っているようです。
そう言われて改めて見てみると、たしかに大体この3つのパターンの組み合わせみたい。
※ちなみに、Facebookのアプリなどでは、9つのアイコンが一画面にでて切り替える、なんていうパターンも見ますね
実際のソース
で、調べながらやってみたのが以下のソース
今回も、InterfaceBuilderを使っています
※ちなみに、XCode 3.2.5
親のViewController
TestApp2ViewController.h
#import <UIKit/UIKit.h> // プロトコル @protocol ColorSelectDelegate -(void)selectColor:(UIColor*)inColor; @end // 使用するデリゲートを宣言 @interface TestApp2ViewController : UIViewController <ColorSelectDelegate> { IBOutletUIButton *changeButton; } @property (nonatomic, retain) IBOutlet UIButton *changeButton; // -(IBAction)change:(UIButton *)sender; @end
親のViewController
TestApp2ViewController.m
#import "TestApp2ViewController.h" #import "blueViewController.h" @implementation TestApp2ViewController @synthesize changeButton; - (void)viewDidLoad { [superviewDidLoad]; } - (void)viewDidUnload { self.changeButton = nil; } - (void)dealloc { [changeButtonrelease]; [superdealloc]; } - (void)didReceiveMemoryWarning { [superdidReceiveMemoryWarning]; } //---------- // ボタンが押されたときの処理 - (IBAction)change:(UIButton *)sender { NSLog(@"change"); blueViewController *blueView = [ [blueViewControlleralloc]initWithNibName:@"blueView"bundle:[NSBundlemainBundle] ]; // デリゲート blueView.colorSelectDelegate_ = self; // モータルビューを開く [selfpresentModalViewController:blueView animated:YES]; } // デリゲートメソッド -(void)selectColor:(UIColor*)inColor { NSLog(@"背景色を変えます"); // 背景色を変更 if (inColor != nil) { self.view.backgroundColor = inColor; } // モータルビューを閉じる [selfdismissModalViewControllerAnimated:YES]; } @end
モータルビュー
blueViewController.h
#import <UIKit/UIKit.h> // デリゲートの宣言 @protocol ColorSelectDelegate; // @interface blueViewController : UIViewController { // デリゲート用 id<ColorSelectDelegate> colorSelectDelegate_; // IBOutletUIButton *setColorButton; } @property (nonatomic, assign) id<ColorSelectDelegate> colorSelectDelegate_; @property (nonatomic, assign) IBOutlet UIButton *setColorButton; -(IBAction)colorSelectButtonAction:(UIButton *) sender; @end
モータルビュー
blueViewController.m
#import "blueViewController.h" #import "TestApp2ViewController.h" @implementation blueViewController; @synthesize colorSelectDelegate_; - (void)didReceiveMemoryWarning { [superdidReceiveMemoryWarning]; } - (void)viewDidUnload { [superviewDidUnload]; self.colorSelectDelegate_ = nil; } - (void)dealloc { [superdealloc]; } - (IBAction)colorSelectButtonAction:(UIButton *) sender { NSLog(@"ここどこ?"); // 背景色決定 UIColor *requestColor = [UIColorredColor]; [colorSelectDelegate_selectColor:requestColor]; } @end