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 vi开发者_JS百科ew, 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. 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.
精彩评论