1

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!

RanLearns
  • 4,086
  • 5
  • 44
  • 81

3 Answers3

1

why don't you just use

 [self presentModalViewController:currentPageController  animated:YES];

and when you want to get back use

    [self dismissModalViewControllerAnimated:YES];

!!!

M.Othman
  • 5,132
  • 3
  • 35
  • 39
  • Thank you for your answer and for providing the actual code for it - this is what I used for this particular app – RanLearns Dec 11 '11 at 22:44
0

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!

Carlos Irano
  • 682
  • 7
  • 8
0

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

Michael Kessler
  • 14,245
  • 13
  • 50
  • 64
  • I don't know... I've just never ever used a navigation controller, I always transition from one viewController to another. Don't navigation controllers ONLY go backwards and forward? What if I wanted a program that went Main Menu -> GamePlay -> Results Screen -> back to Main Menu (without going back through the GamePlay screen)? Is this possible with a navigation controller? I simply transition from the ResultsViewController to the MainMenuViewController the way I am currently doing it. – RanLearns Dec 11 '11 at 10:16
  • @ObjectiveFlash, Yes, navigation controller can do that. You can use `pushViewController:animated:`, `popViewControllerAnimated`, `popToRootViewController` (what you have asked), `popToViewController:animated:`. While using navigation controller you will also receive the `viewWillApear`, `viewWillDisapear` callbacks and few others that you won't receive (without additional code) by using custom transitions. There are very few scenarios that navigation controller doesn't cover and your case is not one of them. – Michael Kessler Dec 11 '11 at 17:25
  • Thank you for your explanation - and does the navigation controller automatically add the bar at the top of the screen for users to click "back" from one view to another? I do not like these automatic bars, I create all my own art for the apps, including back buttons and the like. Is there an easy way to remove it? I am going to mark your answer as correct, although for this particular app I used your second suggestion and opened the second view as a modal view because I only have the two views in this particular application. – RanLearns Dec 11 '11 at 22:44
  • 1
    You have an easy way to hide the navigation bar by `[self.navigationController setNavigationBarHidden:YES];`. But I would suggest to leave it and change its look and feel. You can hide the back button, add your own left (instead of back) and right buttons, setup a titleView if you want to place more than just text in the title, change the tintColor of the navigation bar etc. For iOS5 you can easily modify the UI of all the navigation bars and buttons in the entire app. For all iOS versions you can subclass navigationBar, override the drawRect, and draw an image instead of the regular bar – Michael Kessler Dec 12 '11 at 08:25
  • how do you "open the second view controller as a modal view and dismiss it when done" then ? – Ben Oct 31 '14 at 16:59