如何从窗口中删除视图?

发布于 2024-08-26 13:58:08 字数 857 浏览 5 评论 0原文

我使用 Cocos2D 作为我的主要框架。在某些情况下,我希望 Cocos2D 加载 nib 文件并将其作为视图:

window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
TargetPlayerViewController *myController = [[TargetPlayerViewController alloc]initWithNibName:@"TargetPlayerViewController" bundle:nil];
[window addSubview:[myController view]];
[window makeKeyAndVisible];

这按预期工作,并显示 TargetPlayerViewController。精彩的!

我需要知道的是:加载该视图后,如何让视图自行删除?我尝试了几种不同的方法,但所有方法都会导致程序崩溃。

为了测试,我在视图上设置了一个按钮来触发此方法:

- (IBAction)GTFOnow:(id)sender {
    NSLog(@"GFTO");
    //window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    //[self.view removeFromSuperview];
    //[window makeKeyAndVisible];
}

GTFOnow 是 TargetPlayerViewController 中的一个方法。当它被调用时,当前的子视图(在上面的 Cocos2D 代码中调用)应该从窗口中删除。

I am using Cocos2D for my main framework. In some cases, I want Cocos2D to load a nib file and have that be the view:

window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
TargetPlayerViewController *myController = [[TargetPlayerViewController alloc]initWithNibName:@"TargetPlayerViewController" bundle:nil];
[window addSubview:[myController view]];
[window makeKeyAndVisible];

This works as expected, and shows the TargetPlayerViewController. Wonderful!

What I need to know is: once that view has been loaded, how can I have the view remove itself? I've tried a few different ways, but all of them result in the program crashing.

To test I have a button on the view set up which triggers this method:

- (IBAction)GTFOnow:(id)sender {
    NSLog(@"GFTO");
    //window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    //[self.view removeFromSuperview];
    //[window makeKeyAndVisible];
}

GTFOnow is a method in TargetPlayerViewController. When it is called, the current subview (that was called in the Cocos2D code above) should be removed from the window.

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

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

发布评论

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

评论(1

山有枢 2024-09-02 13:58:08

首先,您不应该仅仅因为要删除子视图而创建新窗口。其次,无论发生其他情况,都不会导致应用程序崩溃。您在哪一堂课上有GTFOnow方法?我想在 TargetPlayerViewController 类中?

First of all, you shouldn't create a new window just because you want to remove a subview. Secondly, whatever else happens, this shouldn't cause the app to crash. In which class do you have the GTFOnowmethod? I suppose in the TargetPlayerViewController class?

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