我正在尝试将新视图加载到现有视图控制器中,但我想从 xib 文件中加载该视图。我的计划是创建第二个 viewController(下面代码中的 viewController1),然后保留它的视图并释放我刚刚创建的那个 viewController。我希望 viewController 会被释放并且视图会一直存在,但这似乎没有发生。
问题 1:如果视图控制器被解除分配,无论视图的保留计数是多少,它的关联视图是否也会被解除分配?在下面的示例代码中,您可以看到视图在突然消失之前的保留计数为 13。
问题2:为什么保留视图会使它的retainCount 增加3?
PageViewController *viewController1 = [[PageViewController alloc] initWithNibName:@"Page1" bundle:nil];
[viewController1.view setUserInteractionEnabled:YES];
NSLog (@"vc retain count: %d", [viewController1 retainCount]); //count=1
NSLog (@"vc view retain count: %d", [viewController1.view retainCount]); //count=4
self.currentPageView=viewController1.view;
NSLog (@"vc retain count: %d", [viewController1 retainCount]); //count=1
NSLog (@"vc view retain count: %d", [viewController1.view retainCount]); //count=7
[viewController1.view retain];
NSLog (@"vc retain count: %d", [viewController1 retainCount]); //count=1
NSLog (@"vc view retain count: %d", [viewController1.view retainCount]); //count=10
[self.view addSubview:viewController1.view];
NSLog (@"vc retain count: %d", [viewController1 retainCount]); //count=1
NSLog (@"vc view retain count: %d", [viewController1.view retainCount]); //count=13
[viewController1 release];
NSLog (@"vc view retain count: %d", [viewController1.view retainCount]);
//objc[3237]: FREED(id): message view sent to freed object=0x538ce0