2010-08-02 45 views
0

我有點困惑,如何建立一個班級,最下面的兩個步驟。如何設置一個類的兩個步驟?

例如,假設我有一款名爲BoardGame的遊戲。其中一個參數在numberOfSquares中。

的類是設置這樣的:

BoardGameSetupViewController > BoardGamePlayViewController > GameEngine

BoardGameSetupViewController已被選定的方塊數後產生BoardGamePlayViewController的一個實例。 BoardGamePlayViewController具有GameEngine類作爲@property,它已被@synthesised。

那麼如何直接從BoardGameSetupViewController設置GameEngine呢?例如,要設置numberOfSquares?

所以在BoardGameSetupViewController中,我想設置引擎的屬性,但它們似乎並沒有傳遞。

BoardGamePlayViewController *boardGamePlayViewController = [[BoardGamePlayViewController alloc] initWithNibName:@"BoardGameView" bundle:nil]; 

// this line doesn't work 
boardGamePlayViewController.gameEngine.numberOfSquares = 12; 

回答

3

您首先需要beofre你做作業boardGamePlayViewController.gameEngine.numberOfSquares創建GameAEngine的實例= 12;所以,

GameEngine *gameEngine = [[GameEngine alloc] init]; 
boardGamePlayViewController.gameEngine = gameEngine; 
boardGamePlayViewController.gameEngine.numberOfSquares = 12; 

應該工作。

相關問題