0

我正在製作一款由4款迷你遊戲組成的iPhone遊戲。每個迷你遊戲都是一個獨立的xib。每場比賽都有自己的菜單和比賽級別。你進入主菜單並選擇你想要加載的遊戲。如何通過重用單個viewcontroller並處理內存警告來加載xibs?

這就是我目前正在加載每個xib。

MainGameViewController.h

@interface MainGameViewController : UIViewController { 

    UIViewController *currentView; 
    int currentViewId; 
} 

@property (nonatomic, retain) UIViewController *currentView; 
- (void)displayView:(int)intNewView; 
@end 

MainGameViewController.m

@implementation MainGameViewController 

@synthesize currentView; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    currentView = [[LogoScreen alloc] init]; 
    [self.view addSubview:currentView.view]; 
} 

- (void)displayView :(int)intNewView 
{ 
    currentViewId = intNewView; 
    currentView = nil; 
    [currentView release]; 
    switch (intNewView) { 
     case SCR_GAME1: 
      currentView = [[Game1View alloc] init]; 
      break; 
     case SCR_GAME1LEVEL: 
      currentView = [[Game1LevelView alloc] init]; 
      break; 
     case SCR_GAME2: 
      currentView = [[Game2View alloc] init]; 
      break; 
     case SCR_GAME2LEVEL: 
      currentView = [[Game2LevelView alloc] init]; 
      break; 
     case SCR_GAME3: 
      currentView = [[Game3View alloc] init]; 
      break; 
     case SCR_GAME3LEVEL: 
      currentView = [[Game3LevelView alloc] init]; 
      break; 
     case SCR_GAME4: 
      currentView = [[Game4View alloc] init]; 
      break; 
     case SCR_GAME4LEVEL: 
      currentView = [[Game4LevelView alloc] init]; 
      break;  
     default: 
      currentView = [[MainView alloc] init]; 
      break; 
    } 

    [self.view addSubview:currentView.view]; 
} 

- (void)dealloc { 
    [currentView release]; 
    currentView = nil; 

    [super dealloc]; 
} 

@end 

這是水平的啓動方式。 Game1View.m

@implementation Game1View 

-init { 
    if (self == [super init]) { 

     MainGameAppDelegate *appdelegate = [[UIApplication sharedApplication] delegate]; 
     isSoundOn = (appdelegate.gvar.soundstate == 1); 
     gamestate = GAME_STATUS_START; 
    } 
    return self; 
} 
... 
... 
- (void)launchgame { 

    MainGameAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
    appDelegate.gvar.currentId = objectid; 
    [appDelegate displayView:SCR_GAME1LEVEL_GAME]; 

} 

- (void)returnToMain { 
    MainGameAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
    [appDelegate displayView:SCR_MAIN]; 
} 

@end 

MainGameAppDelegate.m

@implementation MainGameAppDelegate 
@synthesize window; 
@synthesize viewController; 

- (void) displayView:(int)intNewView { 
    [viewController displayView:intNewView]; 
} 
@end 

目前我得到內存警告,當我把意見,然後應用程序崩潰之間的切換。 我的問題是,這是加載這些視圖的正確方法嗎?我已經發布了我在遊戲壽命期間分配的所有內容,而現在我正在爲記憶力耗盡而感到茫然。任何幫助將非常感激。

回答

3

你不能正確地釋放遊戲視圖,因爲在調用發佈之前,你的代碼實際上會將currentView設置爲零,這意味着你將泄漏每個創建的遊戲視圖。

- (void)displayView :(int)intNewView 
{ 
    currentViewId = intNewView; 
    currentView = nil; 
    [currentView release]; 
    switch (intNewView) { 

你可能想這樣做,而不是:

- (void)displayView :(int)intNewView 
{ 
    currentViewId = intNewView; 
    [currentView release]; 
    currentView = nil; 
    switch (intNewView) { 

而且,你命名你的視圖控制器 「查看」 s是混亂。考慮給他們更多的描述性名稱,如xxxController。

編輯1:

您還需要確保你釋放以前的老遊戲是從視圖層次結構中移除。在釋放視圖之前先調用[currentView.view removeFromSuperview]以將其刪除。

+0

感謝您的快速回復。我認爲你的意思是[currentView.view removeFromSuperview];但如果我在[currentView發佈]之前添加; 它會導致我的一個xib在釋放時有一個EXC_BAD_ACCESS。 – hreaper 2012-03-19 05:15:55

+0

由於您之前已添加子視圖,因此必須通過removeFromSuperview刪除視圖是正常的。嘗試啓用XCode的殭屍來查看額外版本的發生位置。 – futureelite7 2012-03-19 05:27:04

相關問題