How to open UIViewControllers and keepstate withou

2019-08-31 05:25发布

问题:

I have 3 views in my application.

I would like to know how to properly open and load views when buttons are clicked.

Currently when a button is clicked from the first view I open the second view like this

[self dismissViewControllerAnimated:NO completion:nil];

    getPLViewController = [[GetPLViewController alloc] initWithNibName:@"GetPLViewController" bundle:nil];

    UIWindow* keyWindow= [[UIApplication sharedApplication] keyWindow];
    [keyWindow addSubview:getProjectListViewController.view];

    [self presentViewController:getPLViewController animated:NO completion:nil];

And now that the second view is open I open the third like this

currentPLViewController = [[CurrentPLViewController alloc] initWithNibName:@"CurrentPLViewController" bundle:nil];


UIWindow* keyWindow= [[UIApplication sharedApplication] keyWindow];
[keyWindow addSubview: currentPLViewController.view];

[self presentViewController:currentPLViewController animated:NO completion:nil];

I have no idea if this is correct anymore as I have been having trouble with the view showing up on its side if I try to load it before the previous view has finished loading or something along those lines.

This is how I go back from a viewcontroller

[self dismissViewControllerAnimated:NO completion:nil];

So I would like to know is this a correct way of doing it? or is there a better way?

any help would be appreciated.

回答1:

Why you don't use a UINavigationController on your root view ?

You can hide the navigation bar and it will be cleaner and it will match with the Apple guidelines to push views.

Supposing you use XIB. When you add your first view from the AppDelegate, add an UINavigationController and hide this one :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    RootViewController *rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
    [navigationController setNavigationBarHidden:YES];
    self.window.rootViewController = navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

Next on your RootViewController (first view) you add a button and push the SecondViewController :

- (IBAction)displaySecondView
{
    SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    [self.navigationController pushViewController:secondViewController animated:YES];
}

The same in your SecondViewController to push the third one :

- (IBAction)displayThirdView
{
    ThirdViewController *thirdViewController = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
    [self.navigationController thirdViewController animated:YES];
}

and an action to back to the previous view :

- (IBAction)back
{
    [self.navigationController popViewControllerAnimated:YES];
}