简体   繁体   中英

Main View Controller

I'm having problem with my ViewControllers . There are Four of them. Here is the sequence of them. Sorry for the naming of my Views this is just an example.


startViewController ------> menuViewController ------> C1ViewController
                                          \
                                           \ ------> ImportantViewController

startViewController is my intro screen, after its finish, it will [self presentViewController:vc animated:YES completion:NULL]; into my menuViewController .

In my menuViewController are buttons for the two ViewController like the above illustration. Also I presented them in the View with this: [self presentViewController:vc animated:YES completion:NULL]; i return to menuViewController with this [self dismissModalViewControllerAnimated:YES];

What I wanted is to retain ImportantViewController to be the MainViewController-like even if I go to other Views. I dont know if it can be done in ViewController,but to be not removeFromSuperView or just put it Behind all of the other ViewControllers. Is it possible? And How?

Thanks.

Create a property in your MenuViewController.h file, like so...

#import "ImportantViewController.h"

@property (nonatomic, strong) ImportantViewController *importantViewController;

The key is making the property strong, which means your menuViewController will keep a reference to your importantViewController AND increment the retain count behind the scenes.

In your view did load (or anywhere, really), you can setup your ImportantViewController...

@syntheseize importantViewController;


- (void)viewDidLoad
{
     self.importantViewController = [[ImportantViewController alloc] init];
}

Then when you're ready to present it, do it like so:

[self presentViewController:importantViewController Animated:YES];

Then when you eventually dismiss it, your menuViewController will still have a reference to it and it will not be deallocated. You can then show the same controller again later.

Note: This is all assuming you are using Automatic Reference Counting (ARC).

Have importantViewController be the starting view controller, and present all of the other views modally. When you want to switch views, call the dismissModalViewControllerAnimated method and then present a new one. This will work if you have textfields or something that you want to be able to keep the information entered in them without saving/loading it every time you show the view.

//Inside all view controllers aside from the important one

    [self dismissModalViewControllerAnimated:YES];

To show a new controller, either make a modal segue to it and call it from important view controller or programmatically call it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM