RanLearns
RanLearns

Reputation: 4176

addChildViewController and remove it

In my project's main ViewController, once users of the app click the "Start" button, I add the next view to the screen:

UIViewController *nextController = [[GamePlayViewController alloc] initWithNibName:@"GamePlayView" bundle:nil];
[self addChildViewController:nextController];
[self.view addSubview:nextController.view];

Now, when users want to click back to the main menu, what is the proper code to run?

I know I can transition from one viewController to another, like so:

[self transitionFromViewController:currentPageController 
                      toViewController:nextController 
                              duration:0 
                               options:UIViewAnimationOptionTransitionCurlDown
                            animations:nil 
                            completion:^(BOOL finished) { [nextController didMoveToParentViewController:self]; }];

But what if I just want to transition back to the project's main ViewController?

THANKS!

Upvotes: 1

Views: 5538

Answers (3)

Carlos Irano
Carlos Irano

Reputation: 712

You can do that using the code below:

[self willMoveToParentViewController:nil];
[self.view removeFromSuperview];
[self removeFromParentViewController];

Where self is your view controller.

Works fine to me!

Upvotes: 0

M.Othman
M.Othman

Reputation: 5300

why don't you just use

 [self presentModalViewController:currentPageController  animated:YES];

and when you want to get back use

    [self dismissModalViewControllerAnimated:YES];

!!!

Upvotes: 1

Michael Kessler
Michael Kessler

Reputation: 14235

Why are you trying to reinvent the wheel?
Just use a navigation controller and push the second view controller.
Or, if it is not proper in your case to use a navigation controller, then you can open the second view controller as a modal view and dismiss it when done...

Upvotes: 0

Related Questions