Return to root view in IOS

2019-01-28 06:44发布

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.

2条回答
萌系小妹纸
2楼-- · 2019-01-28 07:04

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?

查看更多
劫难
3楼-- · 2019-01-28 07:16

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.

查看更多
登录 后发表回答