Cocoa Objective-C 添加子视图

发布于 2024-10-15 08:59:57 字数 523 浏览 1 评论 0原文

我有一个游戏应用程序(可可,不是可可触摸),我试图在其中添加地图元素。我的应用程序窗口名为 mainWin。我有一个名为 viewGameMap 的 mainWin 子视图,我在 IB 中添加了它。我有一个名为 room.h/room.m 的类,它基本上采用应用程序中生成的尺寸作为房间的宽度和高度。

我可以这样做:

room* thisRoom = [[room alloc] initWithFrame: NSMakeRect(441.0-roomWidth,520.0-roomLength, roomWidth * 10, roomLength * 10)];
[[mainWin contentView] addSubview:thisRoom];

但我真的想将此子视图添加到 viewGameMap,我可以子视图子视图吗?在 IB 中,我注意到子视图没有 contentView,所以我不确定如何将其放置在那里。

在一个相关的问题中,这些房间视图不会是永久性的,所以一旦我完成它们,我如何销毁它们。

谢谢

I have a game application (cocoa, not cocoa touch) where I am trying to add map elements. My app window is named mainWin. I have a subview of mainWin named viewGameMap, which I added in IB. I have a class called room.h/room.m which basically takes dimensions generated in the app for width and height of the room.

I can do this:

room* thisRoom = [[room alloc] initWithFrame: NSMakeRect(441.0-roomWidth,520.0-roomLength, roomWidth * 10, roomLength * 10)];
[[mainWin contentView] addSubview:thisRoom];

But I really want to add this subview to viewGameMap, can I subview a subview? In IB I noticed that a subview doesn't have a contentView so I'm not sure how I would place it there.

In a related question, these room views wouldn't be permanent so how do I destroy them once I am finished with them.

Thanks

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

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

发布评论

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

评论(1

遗失的美好 2024-10-22 08:59:57

首先,如果您在 .h 文件中声明 viewGameMap,如下所示(使用 @property 指令):

@property (nonatomic, retain) IBOutlet UIView   *viewGameMap;

或者甚至只是在实际声明中,如下所示:

IBOutlet UIView *viewGameMap;

那么您应该能够将 viewGameMap 绑定到 IB 中适当的 UIView。这将使您可以直接访问 viewGameMap。

其次,由于您使用 alloc 来实例化您的 Room 对象(注意,类 Room 应该根据约定大写),因此您应对它负责(您拥有它)。但是当您调用 -addSubview: 时,[mainWin contentView] 也拥有 thisRoom。因此,您可以这样做:

room* thisRoom = [[room alloc] initWithFrame: NSMakeRect(441.0-roomWidth,520.0-roomLength, roomWidth * 10, roomLength * 10)];
[viewGameMap addSubview:thisRoom];
[thisRoom release];

稍后,当从 [mainWin contentView] 中删除 thisRoom 时,其引用计数将(除非其他一些引用)降至零,并且最终将被释放。

First, if you declare viewGameMap in your .h file, like this (using @property directive):

@property (nonatomic, retain) IBOutlet UIView   *viewGameMap;

or even just in actual declaration, like this:

IBOutlet UIView *viewGameMap;

then you should be able to bind viewGameMap to appropriate UIView within IB. This will give you direct access to viewGameMap.

Second, since you are using alloc to instantiate your Room object (n.b., class Room should be capitalized per convention), you are responsible for it (you own it). But when you call -addSubview:, then [mainWin contentView] also owns thisRoom. So, you can do this:

room* thisRoom = [[room alloc] initWithFrame: NSMakeRect(441.0-roomWidth,520.0-roomLength, roomWidth * 10, roomLength * 10)];
[viewGameMap addSubview:thisRoom];
[thisRoom release];

Later, when thisRoom is removed from [mainWin contentView], its reference count will (barring some other referencing) drop to zero and it will, ultimately, get deallocated.

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