简体   繁体   中英

How do I add a view controller that already has a parent controller as a child of another view controller

I have 3 view controllers.

ViewcontrollerA is child of ViewcontrollerB

I want to add ViewControllerB as a child of ViewcontrollerC.

Inside ViewcontrollerC.m

ViewcontrollerC.view = ViewcontrollerB.view;

[self addChildViewController:ViewcontrollerB];

[self.view addSubview:ViewcontrollerB.view];

[ViewcontrollerB didMoveToParentViewController:self];

It gives me this error Thread 1: Exception: "child view controller:<ViewcontrollerA: 0x7fee59454a60> should have parent view controller:<ViewcontrollerC: 0x7fee5962f670> but actual parent is:<ViewcontrollerB: 0x7fee694510f0>"

Try this:

ViewcontrollerB.view.frame = self.view.bounds;
[self.view addSubview: ViewcontrollerB.view];
[self addChildViewController: ViewcontrollerB];
[ViewcontrollerB didMoveToParentViewController:self];

You are missing this line: ViewcontrollerB.view.frame = self.view.bounds;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM