简体   繁体   中英

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. 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:)

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. Ie you should be holding on to a reference of that view controller. Then you can call removeFromSuperview on its view.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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