0

As an example, I have baseVC, firstVC and secondVC. BaseVC is the only VC displayed initially, and then upon the user pressing a button in baseVC, they see secondVC presented. When they dismiss secondVC, they see firstVC. When they dismiss firstVC, they see baseVC again.

In other words, I want to present two view controllers at once, one on top of the other. And I want it to animate like displaying a single view controller.

This seems like the initially obvious solution:

//Presents secondVC over firstVC, currently off-screen.
firstVC.presentViewController(secondVC, animated: false, completion: nil)

//Presenting firstVC brings firstVC and secondVC onscreen.
baseVC.presentViewController(firstVC, animated: true) {
    () -> Void in
}

Doing this gives me an error:

Warning: Attempt to present on whose view is not in the window hierarchy!

And what I see instead of my result is simply firstVC presented.

How can I achieve this?

EDIT: These should be both displayed modally.

Andrew
  • 7,693
  • 11
  • 43
  • 81
  • Did you try doing it in the completion handler of the first? Did you like that animation? – Alex Mar 12 '15 at 13:51
  • Doing it there means the first one appears, then the second one either animates in or appears instantly, depending on the animation setting obviously. But no, that's not the result I want. I want to hide firstVC from the user, until after they dismiss secondVC. – Andrew Mar 12 '15 at 13:55
  • Just change the order of your calls, at first present the firstVC and then present the secondVC inside that firstVC – luk2302 Mar 12 '15 at 13:57
  • @luk2302 this animates in firstVC, then immediately displays secondVC. – Andrew Mar 12 '15 at 14:03

2 Answers2

1

Instead of presenting both view controllers, you could do something like this:

var vcs = self.navigationController.viewControllers.mutableCopy();
vcs.addObject(firstVc);
vcs.addObject(secondVc);

self.navigationController.setViewControllers(vcs, animated:false);

Sorry if syntax is bad. I still code in Objective-C

Then give it the modal effect like this: Showing pushviewcontroller animation look like presentModalViewController

Community
  • 1
  • 1
mverderese
  • 5,314
  • 6
  • 27
  • 36
  • Ah, the problem here is not just the animation, but also that it now gives me a series of pushed view controllers, when that's certainly not what I want. I need them to be modal. – Andrew Mar 12 '15 at 14:21
-1

You could try it the following way:

  1. present secondVC animated
  2. if secondVC would be dismissed, present firstVC instead
  3. either remove secondVC from the viewController stack or pop two times from firstVC if it would be dismissed

Since I am only able to code objective-c code as of yet, i can´t provide swift code for that situation.

A general note: try to use push... and pop... in navigationControllers instead of present...

// in your baseVC
- (void)pushButton:(id)sender {
  [self presentViewController:secondVC animated:true completion:nil];
}

// in your secondVC
- (void)backButtonAction:(id)sender {
  [self presentViewController:firstVC animated:true completion:nil];
}

// in your firstVC
- (void)backButtonAction:(id)sender { 
  [baseVC dismissViewControllerAnimated:true completion:nil];
}

Regarding how to know of the backButton event, have a look at Trying to handle "back" navigation button action in iOS

Community
  • 1
  • 1
luk2302
  • 55,258
  • 23
  • 97
  • 137
  • I'm not entirely following, but sure, if you'd be willing, then Objective-C could would absolutely be appreciated. – Andrew Mar 12 '15 at 14:12
  • Ah, the problem here is that I don't want to push and pop. I'm presenting modally on top of a navigation controller. – Andrew Mar 12 '15 at 14:22
  • Ah, you did not yet mention that they are supposed to be modal ^^. let me think... – luk2302 Mar 12 '15 at 14:24
  • changed it again, that should actually present a modal view above baseVC - later on present a modal view above secondVC and then dismiss secondVC, which should cause both modal views to be dismissed – luk2302 Mar 12 '15 at 14:32