xcode 将类/对象添加到子视图

发布于 2024-11-19 13:52:56 字数 800 浏览 0 评论 0原文

希望你能帮忙。我基本上需要将一个对象添加到 UIView 内的 UIView 中。

在界面生成器中,我有我的视图,在其中我有另一个视图,里面有图像(地图)(所有这些都是用界面生成器完成的)。我使用的第二个视图是一个名为“MainGameMapViewController”的类(原因是第一个视图是静态的/包含文本/分数等,而里面的视图是游戏,并且整个视图可以使用“MainGameMapViewController”中的代码进行拖动”)。

我需要将对象添加到“MainGameMapViewController”UIView。

我添加了一个名为“CharMain”的类,并在 MainViewController 中使用了此代码:

-(void) viewWillAppear:(BOOL)animated {

    CharMain* charMain = [[CharMain alloc] initWithFrame:CGRectMake(40, 280, 0, 0)];
    [self.view addSubview:charMain];

}

CharMain 包含绘制矩形并添加图像的代码,这工作正常,但需要将其添加到第二个查看“MainGameMapViewController”。

我尝试在 MainGameMapViewController 文件中添加代码,它显示“错误:请求成员‘视图’不是结构或联合”,我尝试过 [自我添加子视图:charMain];

这没有错误,但图像没有出现,如何使其出现在第二个 UIView 中?谢谢!

Hope you can help. I basically need to add an object to a UIView that is within a UIView.

In interface builder I have my View, and inside that I have another View with an image (map) inside (all done with Interface Builder). The second view I am using a class called "MainGameMapViewController" (reason being is that the first view is static/contains text/scores etc. and the view inside that is the game, and this whole view is draggable with the code in "MainGameMapViewController").

I need to add objects to "MainGameMapViewController" UIView.

I have added a class called "CharMain" and used this code inside the MainViewController:

-(void) viewWillAppear:(BOOL)animated {

    CharMain* charMain = [[CharMain alloc] initWithFrame:CGRectMake(40, 280, 0, 0)];
    [self.view addSubview:charMain];

}

CharMain contains the code that draws a rect and adds an image, this works fine, BUT it needs to be added to the second view "MainGameMapViewController".

I have tried adding the code inside the MainGameMapViewController file, and it says "error: request for member 'view' in something not a structure or union", I have tried
[self addSubview:charMain];

This comes up with no errors, but the image doesnt appear, how do I make this appear within the second UIView? Thanks!

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

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

发布评论

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

评论(1

画尸师 2024-11-26 13:52:56
CharMain* charMain = [[CharMain alloc] initWithFrame:CGRectMake(40, 280, 0, 0)];
//this should be your subview instead of self.view try adding it to your subview's instance.
    [MainGameMapViewControllerInstance.view addSubview:charMain];
CharMain* charMain = [[CharMain alloc] initWithFrame:CGRectMake(40, 280, 0, 0)];
//this should be your subview instead of self.view try adding it to your subview's instance.
    [MainGameMapViewControllerInstance.view addSubview:charMain];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文