从 Cocos2D 类访问 UIView 方法

发布于 2025-01-03 01:04:49 字数 534 浏览 1 评论 0原文

我正在使用 Cocos2D,并且需要访问从另一个类添加到 UIViewController 的 UIView 中的父方法。我的层次结构如下:

分支 1: 窗口 > viewController.view> glView>加入MapsScene>>加入MapsLayer

分支2:窗口> viewController.view> foregroundLabelView

当我的标签类曾经是 Cocos2D 的一部分时,通过执行以下操作可以轻松访问:

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

但现在我需要从joinedMapsLayer调用foregroundLabelView中的方法。这可能不是一个 cocos2D 问题,但我仍然对这类东西感到困惑。

I'm using Cocos2D, and I need to access a parent method in a UIView that is added to a UIViewController from another class. My hierarchy goes like this:

Branch 1: window > viewController.view > glView > joinedMapsScene > joinedMapsLayer

Branch 2: window > viewController.view > foregroundLabelView

When my label class used to be part of Cocos2D, accessing was easy by doing something like this:

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

But now I need to call the method in foregroundLabelView from joinedMapsLayer. It may not be so much of a cocos2D question, but I'm just really confused about this sort of stuff still.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花落人断肠 2025-01-10 01:04:50

如果我理解得很好,你想从 joinMapsLayer 检索 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];

我认为这应该可行。

If I had understood well you want to retrieve the foregroundLabelView instance from joinedMapsLayer. There is a way but I don't know if it's optimal.
You can in your AppDelegate.h instantiate a foregroundLabelView.

Then when you init your foregroundLabelView you assign it to the AppDelegate foregroundLabelView: in your foregroundLabelView.m (you have to import AppDelegate.h), at the end of the init method you can do

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

Then whenever you want you can retrieve it by doing:

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

and then access the method:

[tmp method];

I think this should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文