4

To some this may sound like a daft question. I've searched around, and found little, mostly because I cannot find the appropriate search terms.

Here what I want to do is:

The application begins at view A.

View A launches view B, and view B launches view C.

Is their a way for view C to return directly back to A without dismissing itself and thus exposing B. For example a main menu button.

Andrew S
  • 2,847
  • 3
  • 33
  • 50

2 Answers2

11

You can call popToRootViewControllerAnimated: if you have a UINavigationController. If you specify NO to animate it, then it will just jump back to the root without showing B first.

Magic Bullet Dave
  • 9,006
  • 10
  • 51
  • 81
  • I'm trying [self.navigationController popToRootViewControllerAnimated: NO]; in the IBAction event stub fired by the main menu button. It appears to have no effect but gets executed without raising an error. – Andrew S Jan 27 '12 at 15:23
  • OK, first question do you have a UINavigationController and push your views (via their own view controllers) onto the stack? Every view controller has a navigationController property, but it might be set to nil. – Magic Bullet Dave Jan 27 '12 at 15:26
  • Have a look at http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html to see if this is the effect you are trying to reproduce. – Magic Bullet Dave Jan 27 '12 at 15:29
  • Whe I launch views I'm using [self presentModalViewController: exportMenu animated: YES]. It appears that that self.navigation controller is null. – Andrew S Jan 27 '12 at 15:33
  • OK, so you are presenting modal view controllers on top of other modal view controllers, is that right? It would be helpful if you could explain in a little more detail the effect you are trying to create so I can advise you better. – Magic Bullet Dave Jan 27 '12 at 15:44
  • It seems odd to me that this navigation controller is required.This kind of functionality should come as standard in any IOS view. As there is a stack in memory, one would expect to able to drop to any position in that stack, which would then dismiss all proceeding views from the position chosen. – Andrew S Jan 27 '12 at 15:45
  • I just want to be able to return to the first view in a stack which is the applications main menu. I would like all my views to have a "Main Menu" button to facilitate this. – Andrew S Jan 27 '12 at 15:46
  • Oh and Yes I am presenting modal view controllers on top of each other. – Andrew S Jan 27 '12 at 15:55
  • iOS views are generally managed by a view controller and collections (or stacks) of view controllers are managed by one of the view controller container classes, like a UITabBarController or UINavigationController, and this is what they are designed for. – Magic Bullet Dave Jan 27 '12 at 16:06
  • Thanks for your advice I invented another solution. I have been able to answer my question rather well, I would love to share the solution with you because I believe it's valuable for others to read and quite inventive. However, this stifling rule "Users with less than 100 reputation can't answer their own question for 8 hours" prevents me from doing so, sorry, maybe later eh. :( – Andrew S Jan 27 '12 at 17:12
  • Sure, no problem sounds intriguing. – Magic Bullet Dave Jan 27 '12 at 19:02
1

I have discovered a solution to my problem. Its a bit dirty, (and I''ll probably get shot down in flames for it) but works very well under tests and is very quick to implement. Here's how I did it.

In my app I have a Singleton class called GlobalVars (I use this for storing various global settings). This class holds a boolean called home_pressed and associated accessors (via synthesise). You could also store this value in the application delegate if you wish.

In every view controller with a main menu button, I wire the button to the homePressed IBAction method as follows. First setting the global homePressed boolean to YES, then dismissing the view controller in the usual way, but with NO animation.

-(IBAction) homePressed: (id) sender
{
   [GlobalVars _instance].homePressed = YES;
   [self dismissModalViewControllerAnimated: NO];
}//end homePressed

In every view controller except the main menu I implement the viewDidAppear method (which gets called when a view re-appears) as follows.

-(void) viewDidAppear: (Bool) animated
{
   if ([GlobalVars _instance].homePressed == YES)
   {
      [self dismissModalViewController: NO];
   }
   else
   {
      //put normal view did appear code here/
   }

}//end viewDidAppead

In the mainMenu view controller which is the root of the app, I set the global homePressed boolean to NO in its view did appear method as follows

-(void) viewDidAppear: (Bool) animated
{
   if ([GlobalVars _instance].homePressed == YES)
   {
      [GlobalVars _instance].homePressed == NO;
   }
   else
   {
      //put normal view did appear code here/
   }

}//end viewDidAppear

There, this enables me to go back to the root main menu of my app from any view further down the chain.

I was hoping to avoid this method, but its better than re-implementing my app which is what I'd have to do if I wanted use the UINavigationController solution.

Simple, took me 10 minutes to code in my 9 view app. :)

One final question I do have, would my solution be OK with the HIG?

Andrew S
  • 2,847
  • 3
  • 33
  • 50