我正在尝试添加一个 subview ,然后删除以前的 View 。
这是我的代码:
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 是当前所在的 View 。我希望它关闭该 View ,然后继续添加新 View LetsPlay。
这段代码运行,它加载一个新 View ,但是当我从 LetsPlay 加载另一个 View 时,我可以看到 mainviewcontroller 仍在它后面运行。我想永久解雇它。
我什至不确定我是否正确地处理了这个问题,所以如果我不能,请给我一个如何正确执行此操作的示例。
谢谢:)
最佳答案
您没有以正确的方式进行操作:您正在创建 MainViewController (及其关联 View )的新实例。然后,您尝试从其 super View 中删除这个新创建的 View (称为实例2),而它甚至还没有添加到 View (实例1 已添加)中。这就是为什么您仍然看到 mainviewcontroller。
相反,您需要获取当前正在运行/事件的 MainViewController。 IE。您应该保留该 View Controller 的引用。然后您可以在其 View 上调用removeFromSuperview。
希望这有帮助。
关于objective-c - 正确删除 View 并添加 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7047564/