2011-07-17 27 views
1

我知道這是非常基本的,但我需要澄清。我是嘗試開發iPad應用程序,但有麻煩。我的解釋可能是需要太多的信息,但需要忍受。這就是爲什麼我使用指針?

我有兩個ViewControllers。一個叫做NewGameViewController,另一個叫做GameViewController。在NewGameViewController我顯示GameViewController像這樣:

GameViewController *controller = [[GameViewController alloc] initWithNibName:@"GameViewController" bundle:nil]; 
[self.navigationController pushViewController:controller animated:YES]; 

因此,這在內存中創建GameViewController的一個實例。 GameViewController有一些定義在其中的ivars和方法。其中一個ivars是一個數組,在GameViewController加載時(在viewWillAppear方法中)與座位對象一起歸檔。

我也有一個叫做player的對象。這只是UIView的一個子類。當GameViewController加載它時,最多可以將六個播放器對象添加到子視圖中。我需要每個玩家對象能夠訪問由NewGameViewController加載的GameViewController內存中的同一個實例。我會在我的播放器對象中使用指針來訪問GameViewController的同一個實例嗎?我發現如果我將GameViewController的一個新實例加載到內存中並嘗試使用它,則數組ivar往往是空的,因此不可用。我如何確定我的指向GameViewController的指針指向GameViewController的正確實例?

回答

1

當GameViewController加載它時,最多可將六個播放器對象添加到 子視圖中。

所以,這是你可以做的。在Player上定義一個名爲containedViewController或gameViewController的屬性。

@property (nonatomic, retain) GameViewController *containingViewController; 

不要忘了在實現文件(Player.m)和[containingViewController release];@synthesize containingViewController;dealloc

然後,當你初始化的viewWillAppear方法的玩家,設置屬性:

player1.containingViewController = self; 

這應該採取所有的問題的關心。

+0

啊!!!!!好的!我已經完成了所有這些,但是錯過了最後一行。 player1.containingViewController = self;謝謝。 – dutsnekcirf

+0

沒問題。歡迎來到iOS開發的世界。 –