2012-02-07 60 views
0

我正在使用Cocos2D,並且需要訪問UIView中的父方法,該UIView從另一個類添加到UIViewController。我的層次結構是這樣的:從Cocos2D類訪問UIView方法

分公司1:窗口> viewController.view> glView> joinedMapsScene> joinedMapsLayer

分公司2:窗口> viewController.view> foregroundLabelView

當我的標籤類曾經是Cocos2D中的一部分,訪問是通過做這樣的事情很簡單:

JoinedMapsScene *joinedMapsScene = (JoinedMapsScene*)self.parent; [joinedMapsScene.tetraCounter incTetras:-1];

但現在我需要從joinedMapsLayer調用foregroundLabelView中的方法。這可能不是一個很大的cocos2D問題,但我仍然對這類東西感到困惑。

回答

1

如果我理解的很好,你想從joinedMapsLayer中檢索foregroundLabelView實例。有一種方法,但我不知道它是否最佳。 你可以在你的AppDelegate.h中實例化一個foregroundLabelView。

然後,當你初始化你foregroundLabelView你把它分配給AppDelegate中foregroundLabelView:在你的foregroundLabelView.m(你必須輸入AppDelegate.h),在init方法的最後,你可以做

AppDelegate * delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
delegate.foregroundLabelView = self; 

然後,只要你想,你可以通過做檢索:

ForegroundLabelView * tmp = ((AppDelegate *)[[UIApplication sharedApplication] delegate]).foregroundLabelView; 

,然後訪問方法:

[tmp method]; 

我認爲這應該工作。

+0

所以這意味着我不再可以將foregroundLabelView作爲子視圖添加到viewController.view? – VagueExplanation 2012-02-08 00:01:34

+0

Nono!您可以!但是你也可以從應用程序委託給它分配一個指針。通過這種方式,您可以通過從委託中調用指向它的指針來檢索它! – 2012-02-08 00:31:38

+0

視圖總是一樣的!而且它只有一個!但是你有兩種方法來訪問它:從它的類(和你說的孩子)以及從AppDelegate(從所有類,因爲你可以從所有類中檢索AppDelegate)。 – 2012-02-08 00:35:57