添加子控制器,解决切换子控制器后viewWillAppear:方法不调用问题及移除子控制器

单纯的addChildViewController:,在点击tab切换时并没有移除controller和它的子view,仅需添加如下代码即可解决viewWillAppear:方法不响应的问题:

//Add Child Controller

[self addChildViewController:childController];

[childController didMoveToParentViewController:self];

[childController beginAppearanceTransition:YES animated:NO];

[self.view addSubview:childController.view];

[childController endAppearanceTransition];

如果仅需响应viewWillAppear:,以上代码即可实现。

扩展:移除子控制器

//Remove Child Controller

if([childController parentViewController]) {

                [childController willMoveToParentViewController:nil];

                [childController beginAppearanceTransition:NO animated:NO];

                [childController.view removeFromSuperview];

                [childController endAppearanceTransition];

                [childController removeFromParentViewController];

}

添加子控制器,解决切换子控制器后viewWillAppear:方法不调用问题及移除子控制器

上一篇:爬坑日记 1.axios请求参数为数组时报错


下一篇:springboot启动报错:"Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled."