1. 程式人生 > >三種ViewController跳轉的異同

三種ViewController跳轉的異同

沒有 PE addchild elf ase 事情 控制器 warning 優點

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion

MainVC *mainVC = [[MainVC alloc] init];

[self presentViewController:mainVC animated:YES completion:nil];

這種方式一般出現在需要使用者完成某件事情,如輸入密碼、增加資料等操作後,才能(回到跳轉前的控制器)繼續。例如系統的WIFI連接輸入密碼提示。默認動畫是從下至上。

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated

這種方式一般是使用者瀏覽資料,繼而可以前進到下一個頁面或回到上一個頁面。默認動畫是從右至左。

- (void)addChildViewController:(UIViewController *)childController

這個方法出現在iOS5以後,通過它即使不使用NavigationController也能夠實現view hierarchy。有以下優點:

1.頁面邏輯很清晰,相應的View對應相應的ViewController。
2.當某個子View沒有顯示時,將不會被Load,減少了內存的使用。
3.當內存緊張時,沒有Load的View將被首先釋放,優化了程序的內存釋放機制。

技術分享圖片
#import "ViewController.h"
#import "FirstVC.h"
#import "SecondVC.h"
#import "ThirdVC.h"

@interface ViewController ()
{
    FirstVC *firstVC;
    SecondVC *secondVC;
    ThirdVC *thirdVC;
}

@property (weak, nonatomic) IBOutlet UIView *contentView;

@property (strong, nonatomic) UIViewController *currentVC;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    firstVC = [[FirstVC alloc] init];
    secondVC = [[SecondVC alloc] init];
    thirdVC = [[ThirdVC alloc] init];
    
    [self addChildViewController:firstVC];
    [self addChildViewController:secondVC];
    [self addChildViewController:thirdVC];
    
    [self.contentView addSubview:thirdVC.view];
    
    self.currentVC = thirdVC;
}

- (IBAction)onClick:(id)sender {
    if(self.currentVC==firstVC && [sender tag]==1) {
        return;
    }
    
    if(self.currentVC==secondVC && [sender tag]==2) {
        return;
    }
    
    if(self.currentVC==thirdVC && [sender tag]==3) {
        return;
    }
    
    UIViewController *oldVC = self.currentVC;
    
    switch ([sender tag]) {
        case 1: {
            [self transitionFromViewController:self.currentVC toViewController:firstVC duration:1 options:UIViewAnimationOptionTransitionCurlUp animations:^{
                
            } completion:^(BOOL finished) {
                if(finished) {
                    self.currentVC = firstVC;
                }
                else {
                    self.currentVC = oldVC;
                }
            }];
        }
        break;
        case 2: {
            [self transitionFromViewController:self.currentVC toViewController:secondVC duration:1 options:UIViewAnimationOptionTransitionCurlUp animations:^{
                
            } completion:^(BOOL finished) {
                if(finished) {
                    self.currentVC = secondVC;
                }
                else {
                    self.currentVC = oldVC;
                }
            }];
        }
        break;
        case 3: {
            [self transitionFromViewController:self.currentVC toViewController:thirdVC duration:1 options:UIViewAnimationOptionTransitionCurlUp animations:^{
                
            } completion:^(BOOL finished) {
                if(finished) {
                    self.currentVC = thirdVC;
                }
                else {
                    self.currentVC = oldVC;
                }
            }];
        }
        break;
        default:
            break;
    }
}

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

@end

三種ViewController跳轉的異同