Return to root view in IOS

前端 未结 2 1885
生来不讨喜
生来不讨喜 2020-12-21 03:46

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

相关标签:
2条回答
  • 2020-12-21 04:26

    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?

    0 讨论(0)
  • 2020-12-21 04:45

    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.

    0 讨论(0)
提交回复
热议问题