我正在研究一个应用程序,该应用程序具有一个主视图,该图需要在触摸按钮时产生子视图。因此,当我收到按钮事件时,MainViewController通过调用initWithNibName并将ChildViewController存储在ivar中来衍生子视图。然后我通过附加动画并设置childVC.view.hidden = NO来显示ChildView。为什么我的UIViewController在使用initWithNibName时会得到额外的时间?
这有效,但我注意到在关闭ChildView后,ChildViewController从未获得释放。当我第一次访问子视图时,我意识到ChildVC的保留数从1到2。因此,在笔尖加载内容中的某些东西似乎仍然保留我的ChildVC(除了我期望在对象初始化期间的初始保留)之外。
有人可以帮我弄清楚为什么ChildVC得到保留额外的时间,我怎样才能确保它完全释放时,我想关闭子视图?
编辑:这是一些代码,只是稍微简化。这些是父视图控制器上的方法。
-(IBAction)onLaunchChildButtonTouched:(id)sender
{
m_childViewController = [[ChildViewController alloc] initWithNibName:@"ChildViewController" bundle:nil];
[m_childViewController setParentDelegate:self]; // this is a weak reference
// m_childViewController retain count here is 1, as expected
m_childViewController.view.hidden = YES;
// m_childViewController retain count is now 2, not expected
[self.view addSubview:m_childViewController.view];
[self addTransitionEntrDir:YES]; // code omitted
m_childViewController.view.hidden = NO;
}
-(void)onChildWantsToClose:(id)child
{
NSAssert(child == m_childViewController, @"unexpected childVC");
// if child view is now hidden, we should remove it.
if(m_childViewController != nil && m_childViewController.view.hidden)
{
[m_childViewController.view removeFromSuperview];
[m_childViewController release]; m_childViewController = nil;
// BUG: m_childViewController retain count is still 1 here, so it never gets released
}
}
可能会显示您的实际代码? – Vladimir
显示一些代码将有助于带来A的这个Q. – chown
更新的原始文章与一些代码。 – gga80