2011-10-31 88 views
0

我目前正在創建一本iPad的書。不幸的是,由於這是我的第一個iPad項目,我正以非常規的方式進行編碼。如何切換到另一個XIB並完成以前的XIB?

這是一本20頁的書,我有20個XIB。在每個XIB上,我都會調用下一個XIB,但是我擔心的是當前的XIB沒有被卸載,仍然佔用內存。當我在第10頁左右時,應用程序將因didReceiveMemoryWarning而崩潰。

我切換使用2種不同的方法的意見(我不知道哪一個會更適合我的目的):

#METHOD 1 
-(IBAction)NextPage:(id)sender 
{ 
    nextPage = [[NextPage alloc] initWithNibName:@"NextPage" bundle:nil]; 
    [self.view addSubview:nextPage.view]; 
    [self presentModalViewController:nextPage animated:YES]; 
    [self.view release]; // this was added to hopefully release the current view. 
} 


#METHOD 2 
-(IBAction)NextPage:(id)sender 
{ 
    NextPage *nextpage = [[[NextPage alloc] init] autorelease]; 
    [self presentModalViewController:nextpage animated:YES]; 
} 

每個視圖都有按鈕之類的東西,但我想確保我完全擦除他們從每個頁面調用內存。

我用dealloc嘗試刪除視圖,但是當我檢查內存泄漏並在頁面之間來回切換時,內存會爬起來。

- (void)dealloc { 
    [view release]; // don't know which is which 
    [self.view release]; // is this correct? 
    [super dealloc]; 
} 

任何幫助表示讚賞!提前致謝。

回答

0

如果您只是在書中的頁面之間進行切換,那麼您可能不應該以模態方式進行演示。相反,你可以換掉視圖。

http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/CreatingViews/CreatingViews.html

... transitionFromView:toView:持續時間:選擇:完成:方法 交換了意見換新整個集。

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/clm/UIView/transitionFromView:toView:duration:options:completion

另一種選擇是一個視圖內使用層。

http://www.raywenderlich.com/2502/introduction-to-calayers-tutorial

0

如果您以模態方式呈現下一頁(如您所做的那樣),則當前頁面將不會被釋放。相反,將窗口的rootViewController設置爲新頁面。

+0

我該如何做到這一點? – namkyu

0

的UINavigationController可以更簡單地處理這個問題。看看文檔。此外,從iOS5開始,現在有一種書籍類型的viewController,它可以很好地爲您服務。