从Cocos2D类访问UIView方法

时间:2012-02-07 23:12:06

标签: class uiview methods cocos2d-iphone

我正在使用Cocos2D,我需要访问从另一个类添加到UIViewController的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 个答案:

答案 0 :(得分: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];

我认为这应该有用。