我加载我的主视图控制器的视图。这个主视图控制器然后添加 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
...
}