Cocoa Objective-C 添加子视图
我有一个游戏应用程序(可可,不是可可触摸),我试图在其中添加地图元素。我的应用程序窗口名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,如果您在 .h 文件中声明 viewGameMap,如下所示(使用 @property 指令):
或者甚至只是在实际声明中,如下所示:
那么您应该能够将 viewGameMap 绑定到 IB 中适当的 UIView。这将使您可以直接访问 viewGameMap。
其次,由于您使用 alloc 来实例化您的 Room 对象(注意,类 Room 应该根据约定大写),因此您应对它负责(您拥有它)。但是当您调用 -addSubview: 时,[mainWin contentView] 也拥有 thisRoom。因此,您可以这样做:
稍后,当从 [mainWin contentView] 中删除 thisRoom 时,其引用计数将(除非其他一些引用)降至零,并且最终将被释放。
First, if you declare viewGameMap in your .h file, like this (using @property directive):
or even just in actual declaration, like this:
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:
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.