正确删除视图并添加子视图

发布于 2024-11-29 22:03:57 字数 776 浏览 0 评论 0原文

我尝试添加一个子视图,然后删除以前的视图。

这是我的代码的样子:

    HowToPlay *LetsPlay = [[HowToPlay alloc] initWithNibName:@"HowToPlay" bundle:nil];

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
                       forView:[self view]
                         cache:YES];
[UIView commitAnimations];  



MainViewController *ma = [[MainViewController alloc]init];
[ma.view removeFromSuperview];

[self.view addSubview:LetsPlay.view];

Mainviewcontroller 是当前所在的视图。我希望它消除该视图,然后继续添加新视图 LetsPlay。

这段代码运行,它加载一个新视图,但是当我从 LetsPlay 加载另一个视图时,我可以看到 mainviewcontroller 仍在它后面运行。我想永久解雇它。

我什至不确定我是否正确地处理了这个问题,所以如果我不能,请给我一个如何正确执行此操作的示例。

谢谢 :)

Im trying to add a subview, and then also remove the previous view.

here is what my code looks like:

    HowToPlay *LetsPlay = [[HowToPlay alloc] initWithNibName:@"HowToPlay" bundle:nil];

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
                       forView:[self view]
                         cache:YES];
[UIView commitAnimations];  



MainViewController *ma = [[MainViewController alloc]init];
[ma.view removeFromSuperview];

[self.view addSubview:LetsPlay.view];

The Mainviewcontroller is the view that its currently on. I want it to dismiss that view, then go ahead and add the new view LetsPlay.

This code runs, and it loads a new view, but when i then load another view from LetsPlay i can see that the mainviewcontroller is still running behind it. I want to permanently dismiss it.

Also im not even sure if im going about this correctly, so if im not could you please give me an example of how to do it correctly.

Thanks :)

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

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

发布评论

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

评论(1

终陌 2024-12-06 22:03:57

您没有以正确的方式进行操作:您正在创建 MainViewController (及其关联视图)的实例。然后,您尝试从其超级视图中删除这个新创建的视图(称为实例2),而它甚至还没有添加到视图(实例1 已添加)中。这就是为什么你仍然看到 mainviewcontroller。

相反,您需要获取当前正在运行/活动的 MainViewController。也就是说,您应该保留该视图控制器的引用。然后您可以在其视图上调用removeFromSuperview。

希望这有帮助。

You're not going at it the right way: you're creating a new instance of MainViewController (and also of its associated view). You're then attempting to remove this newly created view (call it instance2) from its superview while it hasn't even been added to a view (instance1 has). This is why you're still seeing mainviewcontroller.

Instead, you need to get a hold of the currently running/active MainViewController. I.e. you should be holding on to a reference of that view controller. Then you can call removeFromSuperview on its view.

Hope this helps.

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