您的位置:首页 > 其它

关于transitionFromViewController的一些用法

2015-09-06 14:55 417 查看
很多时候一个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;
}
}];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: