gpt4 book ai didi

ios - cocoa : Can't remove child view controller

转载 作者:行者123 更新时间:2023-11-28 18:38:36 24 4
gpt4 key购买 nike

我加载主视图 Controller 的 View 。这个主视图 Controller 然后添加 2 个 subview Controller ( Split View:主视图和细节 View )。当我在 init 方法中添加子 VC 后立即记录它们的数量时,我得到“2”作为输出。然后,当我调用 -switchToCommunication 方法并尝试删除详细信息子 VC 时, View 不会改变。但是日志告诉我数组实际上已经从 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
...
}

最佳答案

此代码可以帮助您删除 subview ,对我来说效果很好。self.childViewController 是一个数组,因此您可以从特定索引中删除。

  [[self.childViewControllers objectAtIndex:0] removeFromParentViewController];

关于ios - cocoa : Can't remove child view controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14665607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com