Skip to content

2.3 Custom Container View Controller

tamotamago edited this page Apr 12, 2013 · 2 revisions

UINavigationController Class Reference | View Controller Programming Guide for iOS

iOS 5 から UIViewController の property に childViewControllers がつかされ、UINavigationController や UITabBarController のようなコンテナを自作できるようになりました。

自作コンテナを作ってみましょう

MixiViewController.m

#import "MixiViewController.h"
#import "MixiFirstViewController.h"
#import "MixiSecondViewController.h"

@interface MixiViewController ()

@property (weak, nonatomic) IBOutlet UIView *dummyView;
- (IBAction)pressFirstButton:(id)sender;
- (IBAction)pressSecondButton:(id)sender;
@end

@implementation MixiViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.

    MixiFirstViewController *fvc = [[MixiFirstViewController alloc] init];
    [self addChildViewController:fvc];
    fvc.view.frame = _dummyView.frame;
    [self.view addSubview:fvc.view];
    [fvc didMoveToParentViewController:self];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)transitionFromViewController:(UIViewController *)fromViewController
                   toViewController:(UIViewController *)toViewController
{
    [fromViewController willMoveToParentViewController:nil];
    [self addChildViewController:toViewController];

    toViewController.view.frame = CGRectMake(0, -_dummyView.frame.size.height, _dummyView.frame.size.width, _dummyView.frame.size.height);

    [self transitionFromViewController:fromViewController
                      toViewController:toViewController
                              duration:0.5
                               options:0
                            animations:^{
                                toViewController.view.frame = fromViewController.view.frame;
                                fromViewController.view.frame = CGRectMake(0, 50, _dummyView.frame.size.width, _dummyView.frame.size.height);
                            } completion:^(BOOL finished) {
                                [fromViewController removeFromParentViewController];
                                [toViewController didMoveToParentViewController:self];
                            }];
}


- (IBAction)pressFirstButton:(id)sender
{
    UIViewController *currentVC = self.childViewControllers[0];
    MixiFirstViewController *newVC = [[MixiFirstViewController alloc] init];
    [self transitionFromViewController:currentVC toViewController:newVC];
}

- (IBAction)pressSecondButton:(id)sender
{
    UIViewController *currentVC = self.childViewControllers[0];
    MixiSecondViewController *newVC = [[MixiSecondViewController alloc] init];
    [self transitionFromViewController:currentVC toViewController:newVC];
}

@end

MixiViewController.xib

「イメージ」

  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