官方文档解读
// An array of children view controllers. This array does not include any presented view controllers.
// 子控制器数组,不包括任何已经显示的控制器
@property(nonatomic,readonly) NSArray<__kindof UIViewController *> *childViewControllers API_AVAILABLE(ios(5.0));
/*
If the child controller has a different parent controller, it will first be removed from its current parent
by calling removeFromParentViewController. If this method is overridden then the super implementation must
be called.
如果子控制器拥有一个不同的父控制器,默认会先调用removeFromParentViewController将其移除
如果重写该方法,必须调用[super addChildViewController:childController];
*/
- (void)addChildViewController:(UIViewController *)childController API_AVAILABLE(ios(5.0));
/*
Removes the the receiver from its parent's children controllers array. If this method is overridden then
the super implementation must be called.
从其父控制器的子控制器数组中将其删除
如果重写该方法,必须调用[super removeFromParentViewController];
*/
- (void)removeFromParentViewController API_AVAILABLE(ios(5.0));
/*
This method can be used to transition between sibling child view controllers. The receiver of this method is
their common parent view controller. (Use [UIViewController addChildViewController:] to create the
parent/child relationship.) This method will add the toViewController's view to the superview of the
fromViewController's view and the fromViewController's view will be removed from its superview after the
transition completes. It is important to allow this method to add and remove the views. The arguments to
this method are the same as those defined by UIView's block animation API. This method will fail with an
NSInvalidArgumentException if the parent view controllers are not the same as the receiver, or if the
receiver explicitly forwards its appearance and rotation callbacks to its children. Finally, the receiver
should not be a subclass of an iOS container view controller. Note also that it is possible to use the
UIView APIs directly. If they are used it is important to ensure that the toViewController's view is added
to the visible view hierarchy while the fromViewController's view is removed.
该方法用于同级子控制器的切换(懒得翻译了)
*/
- (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_SWIFT_DISABLE_ASYNC API_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.
这两个方法是用于子控制器切换,如果重写该方法,记得调用super方法
当子控制器从父控制器中移除的时候,参数设置为nil,否则则会成为新的父控制器
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 responsibility 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方法
同样removeFromParentViewController在移除子控制器之前,不会调用willMoveToParentViewControlle,但是会调用didMoveToParentViewController方法
(懒得翻译了)
*/
- (void)willMoveToParentViewController:(nullable UIViewController *)parent API_AVAILABLE(ios(5.0));
- (void)didMoveToParentViewController:(nullable UIViewController *)parent API_AVAILABLE(ios(5.0));
总结
/// 子控制器数组,不包括任何已经显示的控制器
@property(nonatomic,readonly) NSArray *childViewControllers;
/// 添加子控制器,如果子控制器拥有一个不同的父控制器,默认会先调用removeFromParentViewController将其移除
- (void)addChildViewController:(UIViewController *)childController;
/// 从其父控制器的子控制器数组中将其删除
- (void) removeFromParentViewController;
/// 子控制器转化函数
- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^ __nullable)(void))animations completion:(void (^ __nullable)(BOOL finished))completion;
/// 添加子控制器后,该方法会自动调用
- (void)willMoveToParentViewController:(UIViewController *)parent;
/// 删除子控制器后,该方法会自动调用
- (void)didMoveToParentViewController:(UIViewController *)parent;
使用
- 添加子控制器
// 添加子控制器
[superVC addChildViewController:childVC];
// [childVC willMoveToParentViewController: superVC]; [addChildViewController:childVC]会自动调用,可以省略
[superVC.view addSubview:childVC.view];
[childVC didMoveToParentViewController:superVC];
- 删除子控制器
[childVC willMoveToParentViewController:nil];
[childVC.view removeFromSuperview];
[childVC removeFromParentViewController];
// [childVC didMoveToParentViewController:nil]; [childVC removeFromParentViewController]会自动调用,可以省略
// 一开始显示的VC
[self addChildViewController:self.allVC];
[self.containerView addSubview:self.allVC.view];
[self.allVC didMoveToParentViewController:self];
// 保存数据
self.oldVC = self.allVC;
// 切换nowVC
// 添加子控制器
[self addChildViewController:self.nowVC];
// 切换视图
[self transitionFromViewController:self.oldVC toViewController:self.nowVC duration:0 options:UIViewAnimationOptionTransitionNone animations:nil completion:^(BOOL finished) {
if (finished) {
[self.nowVC didMoveToParentViewController:self];
[self.oldVC willMoveToParentViewController:nil];
[self.oldVC removeFromParentViewController];
// 数据保存
self.oldVC = self.nowVC;
}
}];
参考资料
iOS使用addChildViewController
addChildViewController–控制器包控制器解耦
原文地址:https://blog.csdn.net/weixin_46926959/article/details/125979469
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_36990.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。