views:

44

answers:

3

Hi,

I am developing an app but have yet to get the optimal navigation flow working.

To give you an idea of the structure of my app, I have a main view that allows the user to start a new game or view high scores. At the end of a game, I would like to give the user the choice of viewing high scores or going back to the main menu - I have a game over view for this.

The hierarchy looks something like this:

Main View
|
+--Game View
|  |
|  +--Game Over View
|     |
|     +--High Scores View
|
+--High Scores View

I am using the presentModalViewController method to switch from the game view to the game over view. This is fine, but when I want to go back to the main view I have two layers of views to navigate through (I want to close the game over view AND the game view to return straight to the main view). To close views I am using dismissModalViewControllerAnimated but this is not ideal for traversing more than one view. Is there a way of replacing the current model view rather than opening a new one on top?

I hope this makes sense. Please let me know if you need anymore details.

Thanks in advance for any help.

Alan

+1  A: 

If you call -dismissModalViewController:animated: on the root view controller, it will dismiss both layers.

Alexsander Akers
Thanks Alexsander. This works but you can briefly see the middle view before it goes to the root view. Is there a way to avoid this?
Alan Spark
I'm not sure. Sorry…
Alexsander Akers
A: 

It's not elegant, but one way to never see the middle views is to have a transition view (some sort of default view or splash screen) under everything. Then have all views that you want to be swappable dismiss themselves back to the transition view and have that transition view controller present the next modal view to be shown, based on some sort of next-state logic. You'll never be more than 1 view deep, so there won't be any middle views to be seen.

hotpaw2
+1  A: 

In addition to Alexsander's great suggestion, you can eschew the use of presentModelViewController:animated: altogether. Instead, just set the rootViewController property of the active UIWindow.

After setting aViewController as the rootViewController, call [aViewController.view becomeFirstResponder]; to enable it to respond to touch input.


If presentModelViewController:animated: is like calling a method, setting rootViewController is like a goto. I think a "goto" could be more appropriate here because of the cyclic nature of the order in which views can be presented:

Main View -> Game View -> Game Over View -> Main View -> Game View -> Game Over View -> etc.

Jon Rodriguez
Thank you very much Jon, this is very close. I can now get back to the main view directly from the game over view. However, none of my buttons work anymore on the main view (when they work initially). Is there something else I need to do to make the view properly active?
Alan Spark
I just edited my answer in response to your comment; see the 2nd paragraph.
Jon Rodriguez
Thanks Jon. It's working now. Much appreciated.
Alan Spark