Skip to content

1.4 UIViewController2 ModalViewController

tamotamago edited this page Apr 11, 2013 · 1 revision

参考 : UIViewController Class Reference | View Controller Programming Guide for iOS

概要

ModalViewController は一時的にユーザの操作の中に割り込んで表示させるもの。公式ドキュメントには以下のようなケースで使うことを想定している。

  • ユーザから直ちに情報を収集するため
  • 何らかのコンテンツを一時的に表示するため
  • 作業モードを一時的に変更するため
  • デバイスの向きに応じて代替のインターフェイスを実装するため
  • 特殊なアニメーショントランジションを使用する(またはトランジションなしの)新しいビュー 階層を表示するため

UIViewController は一つの ModalView を表示することが可能。そのときに、Modal を表示する ViewController と ModalViewController には親子関係が出来る。

modal1

Source:View Controller Programming Guide for iOS

具体的には親の ViewController の property:presentedViewController に表示されている ModalViewController の参照が代入され、ModalViewController の propterty:presentingViewController に親の ViewController の参照が代入される。

注意:modalViewController property は iOS6 から deplecated なので使用しないようにしましょう。

modal_deprecated

また、ModalViewController の上に ModalViewController をだすこともできる。

modal2

Source:View Controller Programming Guide for iOS

表示方法

MixiPostViewController *postViewController = [[MixiPostViewController alloc] init];
[self presentViewController:postViewController animated:YES completion:nil];

表示オプション

  • modalTransitionStyle 画面遷移の方法
typedef enum {
   UIModalTransitionStyleCoverVertical = 0,
   UIModalTransitionStyleFlipHorizontal,
   UIModalTransitionStyleCrossDissolve,
   UIModalTransitionStylePartialCurl,
} UIModalTransitionStyle;
  • modalPresentationStyle - iPad の場合に表示形式を変更できる
typedef enum {
   UIModalPresentationFullScreen = 0,
   UIModalPresentationPageSheet,
   UIModalPresentationFormSheet,
   UIModalPresentationCurrentContext,
} UIModalPresentationStyle;

消し方と delegate

dismissViewController

[self dismissViewControllerAnimated:YES completion:nil];

このメソッドを呼べば、トップレベルの ModalViewController が dismiss されます。公式ドキュメントには原則として呼び出した ViewControlelr が消すべきと書いてあります。状況に応じて使い分けてください。

dismissViewController

delegate と protocol

MixiChildViewController で閉じるボタンを押したことを MixiViewControlelr が知る必要があります。このようにある VC から VC へ何らかの通知を送る手段の一つとして delegate があります。

delegate

delegate とはあるクラスで処理できない処理を他のクラスに代わりに処理させるパターンです。この場合、MixiChildViewController でボタンが押されたイベントだけキャッチし、MixiChildViewControlelr を閉じる処理は MixiViewControlelr に任せることにします。

MixiChildViewController.h

#import <UIKit/UIKit.h>

@protocol MixiChildViewControllerDelegate <NSObject> // [1] プロトコルの宣言
-(void)didPressCloseButton;
@end

@interface MixiChildViewController : UIViewController

@property (nonatomic, weak) id<MixiChildViewControllerDelegate> delegate; // [2] delegate オブジェクト

- (IBAction)pressClosedButton:(id)sender;

@end

MixiChildViewController.m

-(IBAction)pressClosedButton:(id)sender
{
    // [3] delegate オブジェクトにメッセージを送信
    if([_delegate respondsToSelector:@selector(didPressCloseButton)]){
        [_delegate didPressCloseButton];
    }
}

[1] protocol

プロトコルはメッソド宣言の集合体です。上記では、MixiChildViewController が MixiChildViewControllerDelegate というプロトコルで、他のクラスに任せたいメソッドを宣言しています。

[2] delegate オブジェクト

実際に処理を任せたいクラスのインスタンスが代入されています。id で、MixiChildViewControllerDelegate を採用しているオブジェクトの代入することを条件づけることが出来ます。この場合 MixiViewControlelr が入ります。MixiViewController は自身で MixiChildViewControlelr の参照を持つので、こちらで weak property にしておかないと循環参照が起きます。

[3] delegate オブジェクトにメッセージを送信

respondsToSelector: で delegate 先に delegate method が実装されているかを確認します。実装されていれば、実際にメッセージを送ります。

MixiViewController.h

#import "MixiChildViewController.h"

@interface MixiViewController : UIViewController <MixiChildViewControllerDelegate> // [4] protocol の採用

MixiViewController.m

- (void)pressModalButton:(id)sender
{
    MixiChildViewController *mixiChildViewController = [[MixiChildViewController alloc] init];
    mixiChildViewController.delegate = self; // [5] delegate 先として自身を代入
    [self presentViewController:mixiChildViewController animated:YES completion:nil];
}

// [6] delegate method の実装
#pragma mark - MixiPostViewControllerDelegate methods
-(void)didPressCloseButton
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

[4] protocol の採用

複数ある場合は "," でつなげます。

[5] delegate 先として自身を代入

これを忘れると MixiChildViewController から通知を受け取ることが出来ませんのでお忘れなく。

[6] delegate method の実装

protocol の採用をすることで method の補完が効くようになります。

注意事項

Modal の 表示、非表示アニメーションが同時に起きるとアニメーションの衝突でクラッシュするので気をつけてください。

  1. 導入
  2. Objective C の基礎
  3. メモリ管理
  4. UIViewController1 - UIViewController のカスタマイズ
  5. UIViewController2 - ModalViewController
  6. UIViewController3 - ライフサイクル
  7. UIKit 1 - container -
  8. UINavigationController
  9. UITabController
  10. Custom Container View Controller
  11. UIKit 2 - table view -
  12. UITableView について
  13. UITableViewDelegate, UITableViewDatasource について
  14. custom UITableViewCell の作成
  15. (iOS6) UIRefreshControl について
  16. UIKit 3 - images -
  17. UIImage (CoreGraphics)
  18. UIImageView
  19. Accets Library
  20. CoreImage
  21. UIKit 4 - text -
  22. UILabel
  23. UITextView
  24. KeybordNotification
  25. 非同期処理系
  26. NSURLConnection (json シリアライザ)
  27. Blocks
  28. GCD
  29. View アニメーション、回転対応
  30. View animation
  31. Core animation
  32. lotate
  33. ローカルキャッシュ
  34. UserDefaults
  35. FileManager
  36. CoreData
  37. SQLite
  38. Instruments
  39. leak
  40. time profiler
  41. その他
  42. 単体テスト (GHUnit)
  43. 結合テスト (KIF)
  44. cocoaPods でライブラリ管理

edit sidebar

Clone this wiki locally