willMoveToParentViewController和didMoveToParentView

//
//  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;
                
        }
    }];
}
相關文章
相關標籤/搜索