IOS addChild View Controller方法学習

6030 ワード

  :http://www.it165.net/pro/html/201406/16429.html
//
//  HMTMainViewController.m
//  UIScrollView
//
//  Created by HMT on 14-6-25.
//  Copyright (c) 2014  humingtao. All rights reserved.
//

#import "HMTMainViewController.h"
#import "HMTFirstViewController.h"
#import "HMTSecondViewController.h"
#import "HMTThirdViewController.h"

@interface HMTMainViewController () <UIScrollViewDelegate>

@property (nonatomic ,strong) HMTThirdViewController  *thirdVC;
@property (nonatomic ,strong) HMTFirstViewController  *firstVC;
@property (nonatomic ,strong) HMTSecondViewController *secondVC;

@property (nonatomic ,strong) UIViewController *currentVC;

@property (nonatomic ,strong) UIScrollView *headScrollView;  //        

@property (nonatomic ,strong) NSArray *headArray;

@end

@implementation HMTMainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    self.navigationItem.title = @"    Demo";
    
    self.headArray = @[@"  ",@"  ",@"  ",@"  ",@"  ",@"NBA",@"  "];
    /**
     *   automaticallyAdjustsScrollViewInsets           
     *    "UI  "           ,       
     */
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.headScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, 320, 40)];
    self.headScrollView.backgroundColor = [UIColor purpleColor];
    self.headScrollView.contentSize = CGSizeMake(560, 0);
    self.headScrollView.bounces = NO;
    self.headScrollView.pagingEnabled = YES;
    [self.view addSubview:self.headScrollView];
    for (int i = 0; i < [self.headArray count]; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(0 + i*80, 0, 80, 40);
        [button setTitle:[self.headArray objectAtIndex:i] forState:UIControlStateNormal];
        button.tag = i + 100;
        [button addTarget:self action:@selector(didClickHeadButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.headScrollView addSubview:button];
        
    }
    
    /*
         API   addChildViewController  ,         addSubview ,    [self addChildViewController:child]   sub view   viewController     ViewController    。
                   subview,   addChildViewController subViewController   ;        transitionFromViewController  。         ViewController 。
           :
     
     1.  ,            。   View     ViewController。
     2.    View     ,    Load,        。
     3.      ,  Load View      ,            。
     */
    
    /**
     *   iOS5 ,ViewController           :
     *  addChildViewController:
     *  removeFromParentViewController
     *  transitionFromViewController:toViewController:duration:options:animations:completion:
     *  willMoveToParentViewController:
     *  didMoveToParentViewController:
     */
    self.firstVC = [[HMTFirstViewController alloc] init];
    [self.firstVC.view setFrame:CGRectMake(0, 104, 320, 464)];
    [self addChildViewController:_firstVC];
    
    self.secondVC = [[HMTSecondViewController alloc] init];
    [self.secondVC.view setFrame:CGRectMake(0, 104, 320, 464)];
    
    self.thirdVC = [[HMTThirdViewController alloc] init];
    [self.thirdVC.view setFrame:CGRectMake(0, 104, 320, 464)];
    
    //    ,     (    ,        addSubview)
    [self.view addSubview:self.firstVC.view];
    self.currentVC = self.firstVC;
    
}

- (void)didClickHeadButtonAction:(UIButton *)button
{
    //             ,    
    if ((self.currentVC == self.firstVC && button.tag == 100)||(self.currentVC == self.secondVC && button.tag == 101.)) {
        return;
    }else{
    
        //    2 ,    ,     
        switch (button.tag) {
            case 100:
                [self replaceController:self.currentVC newController:self.firstVC];
                break;
            case 101:
                [self replaceController:self.currentVC newController:self.secondVC];
                break;
            case 102:
                //.......
                break;
            case 103:
                //.......
                break;
            case 104:
                //.......
                break;
            case 105:
                //.......
                break;
            case 106:
                //.......
                break;
                //.......
            default:
                break;
        }
    }

}

//          
- (void)replaceController:(UIViewController *)oldController newController:(UIViewController *)newController
{
    /**
     *                   
     *  transitionFromViewController:toViewController:duration:options:animations:completion:
     *  fromViewController                         
     *  toViewController                   
     *  duration                    (    ,old friend   O(∩_∩)O)
     *  options                     (  ,      ,    API)
     *  animations                      
     *  completion                  
     */
    
    [self addChildViewController:newController];
    [self transitionFromViewController:oldController toViewController:newController duration:2.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {
                
        if (finished) {
                    
            [newController didMoveToParentViewController:self];
            [oldController willMoveToParentViewController:nil];
            [oldController removeFromParentViewController];
            self.currentVC = newController;
                
        }else{
                    
            self.currentVC = oldController;
                
        }
    }];
}