我加载了主视图控制器的视图。然后,此主视图控制器添加2个子视图控制器(拆分视图:主视图和详细信息)。当我在init方法中添加它们后立即记录子VC的数量时,我得到'2'作为输出。 然后,当我调用-switchToCommunication方法并尝试删除详细信息子VC时,视图不会更改。但是日志告诉我,阵列实际上已经从2个子VC缩小到1。
什么事?
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
[super init];
//add master view controller as childVC
self.test2 = [test2 new];
[self addChildViewController:self.test2];
self.test2.view.frame = CGRectMake(0, 0, 256, 768);
[self.view addSubview:self.test2.view];
[self.test2 didMoveToParentViewController:self];
//add detail view controller as childVC
self.detail1Vc = [EADetailSupportViewController new];
[self addChildViewController: self.detail1Vc];
self.detail1Vc.view.frame = CGRectMake(284, 0, 740, 768);
[self.view addSubview: self.detail1Vc.view];
[self.detail1Vc didMoveToParentViewController:self];
NSLog(@"Child VC in childVC array: %d", [self.childViewControllers count]);
- (void) switchToCommunication {
//remove currently active detail view controller from parent view
NSLog(@"Child VC in childVC array: %d", [self.childViewControllers count]);
[[self.childViewControllers lastObject] willMoveToParentViewController:nil];
[[[self.childViewControllers lastObject] view] removeFromSuperview];
[[self.childViewControllers lastObject] removeFromParentViewController];
NSLog(@"Child VC in childVC array: %d", [self.childViewControllers count]);
NSLog(@"pushed");
//add communication detail view controller as child view controller
...
}
答案 0 :(得分:2)
此代码可以帮助您删除子视图,对我来说工作正常.self.childViewControllers是一个数组,因此您可以从特定索引中删除。
[[self.childViewControllers objectAtIndex:0] removeFromParentViewController];
答案 1 :(得分:2)
试试这个
[self.childviewcontroller willMoveToParentViewController:nil;
[self.childviewcontroller removeFromParentViewController];
[self.childviewcontroller.view removeFromSuperview];
答案 2 :(得分:0)
我想我想通了:我没有转发指向子VC的指针,而是创建了一个新的父vc类,从而导致新的初始化(重新安置所有子VC)。你应该总是将指向包含类的指针转发给子VC!