Chlebta
Chlebta

Reputation: 3110

Dimiss modal View Controller in background

I got View A present -> Modal View B, B present -> Modal View C Modal View D -> Modal View E . It's a main View A and then succession of Modal view.
So each time I present new modal C or D I want to dismiss the previous one (In this way when I close the new presented Modal It will show me The Main view A always not the previous Modal).

I've tried this code :

if (_openNextView) {
        if ([[NSUserDefaults standardUserDefaults] stringForKey:@"generatedCode"]) {
            NSLog(@"generated Code %@ : ", [[NSUserDefaults standardUserDefaults] stringForKey:@"generatedCode"]);
            NSLog(@"phone Number %@ : ", [[NSUserDefaults standardUserDefaults] stringForKey:@"phoneNumber"]);

            // Present C View
            RegisterSecondViewController *registerSecond = [[RegisterSecondViewController alloc] initWithNibName:@"RegisterSecondViewController" bundle:nil];

            //[self presentNatGeoViewController:registerSecond];

            [self presentViewController:registerSecond animated:YES completion:nil];
        } else {
            RegisterFirstViewController *registerFirst = [[RegisterFirstViewController alloc] initWithNibName:@"RegisterFirstViewController" bundle:nil];

             //present D view
            //[self presentNatGeoViewController:registerFirst];
            [self presentViewController:registerFirst animated:YES completion:nil];
        }
// Dismiss privious View (the current view before presenting new one)
    [self.parentViewController dismissViewControllerAnimated:YES completion:nil];

Also I've o add this code in the new presented Modal view

[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];

But didn't worked and the view is not dismissed

Upvotes: 1

Views: 1272

Answers (3)

Wain
Wain

Reputation: 119031

I'd modally present a UINavigationController and use it to present the content views. Then, when you currently present new modal views instead you would just set the viewControllers (animated) of the navigation controller.

This will give you an animated view of progression through your setup process but won't allow the user to go back and will deallocate the VCs when they're complete.

Upvotes: 1

Bannings
Bannings

Reputation: 10479

You can dismiss the current vc after that presenting a new vc. Try this:

[self presentViewController:vc animated:YES completion:^{
    if (self.presentingViewController != nil) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:NO completion:nil];
        });
    }
}];

Upvotes: 0

Teo
Teo

Reputation: 3442

UIViewController has a property named presentedViewController :

presentedViewController
Property The view controller that is presented by this view controller, or one of its ancestors in the view controller hierarchy. (read-only)

This means, you can only present one modal controller at a given time. In order to present another one, you need to dismiss the current one first.

If you want to have a custom navigation, I recommend you to create a custom container view controller since the normal modal presentation won't fit your use case. Here is a guide from Apple.

Upvotes: 0

Related Questions