9

我正在向父视图控制器添加一个子视图控制器,一切都按预期工作,除了子视图控制器没有触发其通常的回调。例如,像 viewWillAppear(animated) 这样的东西永远不会被调用。我调查了一下,认为这可能是因为我没有分别在子视图控制器上调用 willMoveToParentViewController 和 didMoveToParentViewController 之前和之后将其添加到父级。不幸的是,修复它没有任何区别。有谁知道我如何将其连接起来,以便在我执行 addChildViewController 和 removeChildViewController 时触发常规回调?添加 childViewController 后,我还将其视图作为子视图添加到父视图控制器的视图中。在任何一点(addChildViewController &

缺口

4

7 回答 7

11

viewWillAppearviewDIdAppear在您将 ViewControler 视图添加到视图层次结构时自动调用

viewWillDisappearviewDidDisappear当您从视图层次结构中删除 ViewControler 视图时自动调用

也许没有调用此方法是因为您在显示主视图控制器之前添加了子控制器视图,因此您的主视图不在视图层次结构本身中?

当您的主视图控制器出现或消失时,您应该在相应的方法中为孩子调用此方法。

于 2013-12-25T05:38:53.470 回答
8

不确定这是否适用于您的情况,但尝试手动向您的孩子发送外观回调方法。

从苹果的视图控制器包含文档

但是,有时默认行为可能会以对您的容器没有意义的顺序发送这些事件。例如,如果多个子项同时更改其视图状态,您可能希望合并更改,以便外观回调以更合乎逻辑的顺序同时发生。为此,您需要修改容器类以接管外观或旋转回调的责任。

如果您想要更细粒度的控制,它建议手动将外观回调转发给您的孩子:

// From the container view controller
- (BOOL) shouldAutomaticallyForwardAppearanceMethods
{
    return NO;
}

-(void) viewWillAppear:(BOOL)animated
{
    [self.child beginAppearanceTransition: YES animated: animated];
}

-(void) viewDidAppear:(BOOL)animated
{
    [self.child endAppearanceTransition];
}

-(void) viewWillDisappear:(BOOL)animated
{
    [self.child beginAppearanceTransition: NO animated: animated];
}

-(void) viewDidDisappear:(BOOL)animated
{
    [self.child endAppearanceTransition];
}
于 2015-03-04T16:12:34.320 回答
7

试试这个:

childController.willMoveToParentViewController(self)
childController.beginAppearanceTransition(true, animated: true)

添加到您的视图控制器后

于 2015-09-03T09:03:14.063 回答
4

刚刚尝试在父视图控制器的 viewDidLoad 中执行以下操作,它似乎有效

ChildExperimentViewController *child = [[ChildExperimentViewController alloc]init];
[self addChildViewController:child];
[self.view addSubview:child.view];
[child didMoveToParentViewController:self];
于 2013-12-24T02:33:50.983 回答
0

Chourobin用 Swift 回答

override var shouldAutomaticallyForwardAppearanceMethods: Bool {
    return false
}

override func viewWillAppear(_ animated: Bool) {
    self.childViewControllers[self.segmentView.selectedSegmentIndex].beginAppearanceTransition(true, animated: animated)
}

override func viewDidAppear(_ animated: Bool) {
    self.childViewControllers[self.segmentView.selectedSegmentIndex].endAppearanceTransition()
}

override func viewWillDisappear(_ animated: Bool) {
    self.childViewControllers[self.segmentView.selectedSegmentIndex].beginAppearanceTransition(false, animated: animated)
}

override func viewDidDisappear(_ animated: Bool) {
    self.childViewControllers[self.segmentView.selectedSegmentIndex].endAppearanceTransition()
}

PS 因为我没有将对子 VC 的引用保留为属性或变量。我更喜欢使用当前选择的段索引来访问它。SegmentView 是第三方UISegmentControl

于 2017-09-11T09:46:52.180 回答
0
let vc = ChildVC.init()
vc.frame = self.view.bounds
self.addChildViewController(vc)
self.view.addSubview(vc.view)
vc.willMove(toParentViewController: self)
vc.didMove(toParentViewController: self)
于 2017-09-11T09:32:14.287 回答
-4

You can manual call the child view controller's viewWillAppear in parectViewController,code like this

-(void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    [selectedViewController viewWillAppear:animated];
}

if you have add a navigationController as childViewController you can add code like this in navigationController's delegate

-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated 
{
    [viewController viewWillAppear:animated];
}

if you have add a tabBarController as childViewController you can add code like this in tabBarController's delegate and super view

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 
    [self.tabBarController.selectedViewController viewWillAppear:animated]; 
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { 
    [viewController viewWillAppear:NO]; 
}

I don't know if this can solve your problem,hope be able to help you.

于 2013-12-24T02:53:52.810 回答