简体   繁体   English

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

[英]Properly remove view, and add subview

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. Mainviewcontroller 是它当前打开的视图。 I want it to dismiss that view, then go ahead and add the new view LetsPlay.我希望它消除该视图,然后是 go 并添加新视图 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.这段代码运行,它加载了一个新视图,但是当我从 LetsPlay 加载另一个视图时,我可以看到 mainviewcontroller 仍在它后面运行。 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:)谢谢:)

You're not going at it the right way: you're creating a new instance of MainViewController (and also of its associated view).你的方法不对:你正在创建 MainViewController 的一个实例(以及它的关联视图)。 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).然后,您尝试从其父视图中删除这个新创建的视图(称为 instance2),而它甚至还没有被添加到视图中(instance1 有)。 This is why you're still seeing mainviewcontroller.这就是您仍然看到 mainviewcontroller 的原因。

Instead, you need to get a hold of the currently running/active MainViewController.相反,您需要掌握当前正在运行/活动的 MainViewController。 Ie you should be holding on to a reference of that view controller.即,您应该保留该视图 controller 的参考。 Then you can call removeFromSuperview on its view.然后你可以在它的视图上调用 removeFromSuperview 。

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM