关于transitionFromViewController的一些用法

很多时候一个UIViewController有很多的小的子view,这些view很多时候被覆盖在最后,我们一般在最外层addSubview 添加view这些view大多不会一直处于在界面上显示,一般只有在某些时候才会显示,虽然这些view很少出现,但却常常把他们放在内存中,当内存警告时,只能删除他们


这个时候用transitionFromViewController  


对于那些暂时不需要显示的subview,通过addchildviewcontroller 把subviewcontroller加进去,同时调用self addchildviewcontroller:需要显示时再调用transitionFromViewController方法。将其添加进入底层的ViewController中。

相应的view对应相应的controller

当某个子view没有显示的时候,将不会load,减少内存的使用,

当内存紧张的时候,没有被load的view辉被释放,优化了内存,

[self transitionFromViewController:_currentViewController toViewController:third duration:0.1 options:UIViewAnimationOptionTransitionNone animations:^{

        

        

    } completion:^(BOOL finished) {

        if (finished) {

            _currentViewController = third;

        } else {

            _currentViewController = oldViewController;

        }

    }];

参数1,当前的viewcontroller  ,参数二,显示的controller 参数三,过度时间,参数4,过度效果,  block语句块,

过度完成后设置当前的controller



简单的使用如下:


- (void)viewDidLoad {

    [super viewDidLoad];

    

    

   first = [[FirstViewController alloc]init];

    

    [self addChildViewController:first];


    

    second = [[SecondViewController alloc]init];

    [self addChildViewController:second];


    

    third  =[[ThirdViewController alloc]init];

    [self addChildViewController:third];

    

    [_AddViews addSubview:first.view];

    

    _currentViewController = first;

    

    

    

}

- (IBAction)ThreeBtn:(id)sender {

        NSLog(@"%@",_currentViewController);

    if (_currentViewController == third ) {

        return;

    }

    UIViewController *oldViewController = _currentViewController;

    [self transitionFromViewController:_currentViewController toViewController:third duration:0.1 options:UIViewAnimationOptionTransitionNone animations:^{

        

        

    } completion:^(BOOL finished) {

        if (finished) {

            _currentViewController = third;

        } else {

            _currentViewController = oldViewController;

        }

    }];


    

}


- (IBAction)SecondBtn:(id)sender {

        NSLog(@"%@",_currentViewController);

    if (_currentViewController == second ) {

        return;

    }

    UIViewController *oldViewController = _currentViewController;


    [self transitionFromViewController:_currentViewController toViewController:second duration:0.1 options:UIViewAnimationOptionTransitionNone animations:^{

        

        

    } completion:^(BOOL finished) {

        if (finished) {

            _currentViewController = second;

        } else {

            _currentViewController = oldViewController;

        }

    }];


    

}


- (IBAction)OneBtn:(id)sender {

    NSLog(@"%@",_currentViewController);


    if (_currentViewController == first ) {

        return;

    }


    UIViewController *oldViewController = _currentViewController;

    [self transitionFromViewController:_currentViewController toViewController:first duration:0.1 options:UIViewAnimationOptionTransitionNone animations:^{

        

        

    } completion:^(BOOL finished) {

        if (finished) {

            _currentViewController = first;

        } else {

            _currentViewController = oldViewController;

        }

    }];

    

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值