我正在研究一個應用程序,該應用程序具有一個主視圖,該圖需要在觸摸按鈕時產生子視圖。因此,當我收到按鈕事件時,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