Tim Windsor Brown
Tim Windsor Brown

Reputation: 4089

How to present a second ViewController & dismiss the first

using the following code in the parent ViewController, I want to present a second view ontop of the first, then dismiss the first:

// Animates the next questionViewController using the first questionViewController
[previousView presentViewController:nextQuestionViewController animated:YES completion:nil];

// Dismiss the first questionViewController
[previousView dismissViewControllerAnimated:NO completion:nil];

When run, the second view is presented, but the first view will not dismiss.

Upvotes: 9

Views: 27284

Answers (5)

Nate Petersen
Nate Petersen

Reputation: 888

It might be better to use an "unwind segue":

https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/UsingSegues.html#//apple_ref/doc/uid/TP40007457-CH15-SW8

https://developer.apple.com/library/archive/technotes/tn2298/_index.html

It allows you to dismiss multiple view controllers at the same time, without having to know how many there are in the stack. And without the presented view controller having special knowledge of where it needs to navigate back to (i.e. you don't have to reach out directly to the window's root view controller).

Let's say you have some root view controller called VC1, the first modal is VC2, and the second modal is VC3.

In VC1 you would implement an IBAction called (for instance) unwindToRoot. Then in the storyboard for VC3, you wire up your Done button to the Exit object and choose the unwindToRoot action.

When that button is pressed, the system will dismiss all the view controllers it needs to bring you back to VC1.

So basically you are just letting all these VC stack up, and when you are all done you are dismissing everything to return to the root VC.

Upvotes: 0

prewett
prewett

Reputation: 1647

It seems that it is not possible to go from B to C without showing A briefly, which looks unprofessional. However, you can put a black subview over top of A until you've animated to C.

For code, see my answer at https://stackoverflow.com/a/45579371/218226

Upvotes: -2

Serge Maslyakov
Serge Maslyakov

Reputation: 1410

I did next (self - is your old controller):

UIStoryboard *storyboard = self.storyboard;

[self dismissViewControllerAnimated:YES
                         completion:^{
        UIViewController *newController = [storyboard instantiateViewControllerWithIdentifier:@"newControllerStoryboardId"];
        newController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:newController animated:YES completion:nil];
    }];

Upvotes: 2

Bern11
Bern11

Reputation: 255

You would need to first dismiss the "previousView" & then present the "nextQuestionViewController":

// Dismiss the first questionViewController
[previousView dismissViewControllerAnimated:NO completion:nil];

// Animates the next questionViewController using the first questionViewController
[previousView presentViewController:nextQuestionViewController animated:YES completion:nil];

Upvotes: 12

ader
ader

Reputation: 5393

try

[self dismissViewControllerAnimated:NO completion:nil];

failing that:

[self.navigationController popViewControllerAnimated:YES];

Upvotes: 5

Related Questions