代码下载地址:https://github.com/JLHuu/TestChildVcs.git
Xcode虽然提供了像tabbarViewcontroller这样的类,但是依然满足不了需求。还好苹果为我们提供了ChildViewController这个api,让我们可以随心所欲的定制自己需要的ViewController。
本文主要以一个例子来见证如何使用childViewcontroller来自定义ViewControler。
首先创建一个BaseViewController的基类,在他的viewDidLoad,viewWillAppear,viewWillDisappear方法里面写上这段代码
NSLog(@"%s,%@",__FUNCTION__,NSStringFromClass([self class]));
以便,后续观察各个界面跳转的生命周期是否如预期那样。
然后创建三个子viewcontroller和一个父ViewControler都继承自BaseViewController,分别命名为FViewController,SViewController,TViewController和ParentViewController。在三个子视图控制器的viewDidLoad中设置三个视图颜色分别为红,绿,蓝色,以便区别。
在父控制器中我们定义几个属性:
@property(nonatomic,strong)UISegmentedControl *control;
@property(nonatomic,strong)FViewController *fvc;
@property(nonatomic,strong)SViewController *svc;
@property(nonatomic,strong)TViewController *tvc;
@property(nonatomic,strong)BaseViewController *currentvc;// 当前子视图vc
UISegmentedControl是用来点击控制跳转的
在viewdidload中对视图进行初始化,只add一个子视图控制器,这样父控制器只需要管理一个控制器。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_control = [[UISegmentedControl alloc] initWithItems:@[@"FVC",@"SVC",@"TVC"]];
_control.backgroundColor = [UIColor grayColor];
_control.frame = CGRectMake((CGRectGetWidth(self.view.frame)-150)/2, 30, 150, 30);
[_control addTarget:self action:@selector(ChangedSegment:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:_control];
[_control setSelectedSegmentIndex:0];//默认第一个
_fvc = [[FViewController alloc] init];
[_fvc.view setFrame:self.view.bounds];
// 设置完view的fream,就会走viewdidload
// 其他的vcs在视图控制器交换完成的时候就会进行视图加载
_svc = [[SViewController alloc] init];
_tvc = [[TViewController alloc] init];
[self addChildViewController:_fvc];// 默认是第一个vc
[self.view addSubview:_fvc.view];
_currentvc = _fvc;
[self.view bringSubviewToFront:_control];
}
视图交换主要用到几个方法
- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^ __nullable)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(5_0);
- (void)willMoveToParentViewController:(nullable UIViewController *)parent NS_AVAILABLE_IOS(5_0);
- (void)didMoveToParentViewController:(nullable UIViewController *)parent NS_AVAILABLE_IOS(5_0);
第一个方法是对俯视图的两个子视图控制器的进行切换过渡。
下两个方法是在容器类视图控制器(父视图控制器)在对子视图控制器进出容器过渡时调用的。
贴一段注释:
/*
These two methods are public for container subclasses to call when transitioning between child
controllers. If they are overridden, the overrides should ensure to call the super. The parent argument in
both of these methods is nil when a child is being removed from its parent; otherwise it is equal to the new
parent view controller.
addChildViewController: will call [child willMoveToParentViewController:self] before adding the
child. However, it will not call didMoveToParentViewController:. It is expected that a container view
controller subclass will make this call after a transition to the new child has completed or, in the
case of no transition, immediately after the call to addChildViewController:. Similarly
removeFromParentViewController: does not call [self willMoveToParentViewController:nil] before removing the
child. This is also the responsibilty of the container subclass. Container subclasses will typically define
a method that transitions to a new child by first calling addChildViewController:, then executing a
transition which will add the new child's view into the view hierarchy of its parent, and finally will call
didMoveToParentViewController:. Similarly, subclasses will typically define a method that removes a child in
the reverse manner by first calling [child willMoveToParentViewController:nil].
*/
大概意思就是说:这两个方法在重写是,我们要知道他们在哪儿调用的。在容器控制器调用addChildViewController时在加入子视图控制器前会自动调用[子视图控制器 willMoveToParentViewController:父视图控制器],但是不会调用didMoveToParentViewController这个方法。这个方法我们应该在过渡完成后调用,或者在没有使用过渡时在addChildViewControler后立即调用。而[child willMoveToParentViewController:nil]这个方法则是在视图过渡完成后定义的和didMoveToParentViewController相反的方法,也就是说在成功过渡后再removeFromParentViewController之前调用即可。
点击segment控制代码
if (sender.selectedSegmentIndex == 0 && _currentvc != _fvc) {
[self transitionVC:_currentvc toVC:_fvc];
}
if (sender.selectedSegmentIndex == 1 && _currentvc != _svc) {
[self transitionVC:_currentvc toVC:_svc];
}
if (sender.selectedSegmentIndex == 2 && _currentvc != _tvc) {
[self transitionVC:_currentvc toVC:_tvc];
}
视图过渡代码如下:
- (void)transitionVC:(BaseViewController *)currentvc toVC:(BaseViewController *)newvc
{
// 加入子视图vc才可交换
[self addChildViewController:newvc];
// 子视图vc交换
[self transitionFromViewController:currentvc toViewController:newvc duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:NULL completion:^(BOOL finished) {
if (finished) {//交换完成
// 移除老的vc推出新的vc
[newvc didMoveToParentViewController:self];
[currentvc willMoveToParentViewController:nil];
[currentvc removeFromParentViewController];
self.currentvc = newvc;
}else{// 交换失败
// 移除newvc
self.currentvc = currentvc;
[newvc willMoveToParentViewController:nil];
[newvc removeFromParentViewController];
}
}];
[self.view bringSubviewToFront:_control];
}
简书不支持视频上传,所以效果和log可以下代码运行看下:这里