0

I have a 1st VC (view controller) that should be modal, and it has a child modal VC that sometimes should be presented as soon as 1st VC will appear, but sometimes not. So from a root VC I want to present 1st VC, and over the 1st VC present the child modal VC. But the user should only see the child VC modal transition (not the 1st VC).

AFAIK the 1st VC can only present a modal VC after viewDidAppear:, so I don't know how to make this possible, since when viewDidAppear: is called, the 1st VC is already visible to the user.

Don't want the user to see 2 modal transitions one after the other, but just the last modal transition, the child's one.

Any hint?

Fede Mika
  • 1,894
  • 1
  • 17
  • 18

3 Answers3

5

I figured out the simplest solution to this if you still haven't found a suitable one. You can use a UINavigationController to hold the 2 nested view controllers you are trying to display modally.

In the function that is meant to display the modal views you could do something like:

- (IBAction)showView3
{

    ViewController2 *new2 = [[ViewController2 alloc] init];   
    ViewController3 *new3 = [[ViewController3 alloc] init];

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:new2];
    nav.navigationBarHidden = YES;
    [nav pushViewController:new3 animated:NO];
    [self presentModalViewController:nav animated:YES];
}

Then function in ViewController3 to dismiss it would have:

[self.navigationController popViewControllerAnimated:YES];

And the one in ViewController2 would have:

[self dismissModalViewControllerAnimated:YES];

The only issue I can see with this is aesthetics, as by default the transition from view3 to view2 is a horizontal animation but the one from view2 back to view1 is vertical. You could of course change that as well to make them all horizontal, or all vertical, or however you want.

Dima
  • 22,982
  • 5
  • 52
  • 82
  • 1
    Hi lolcat, this works pretty well! The only problem is that I had to simulate a "modal dismiss" for the new3 pushed view. I did it using CATransition as suggested [here](http://stackoverflow.com/a/5660278/333533), but still cannot avoid a small fadeout in the transition, although nothing serious. Thanks for your ideas! – Fede Mika May 16 '12 at 19:23
  • Great! I did just have another idea but have not tried it out. What if ViewController3 is actually presented modally onto the navigationcontroller before you present the navigation controller modally? This would provide you with the animation you want by default in all cases, I'm just not sure offhand if presenting a modal view onto a view not currently on the screen would work. – Dima May 16 '12 at 19:32
  • Another note: I found out recently that the modalviewController functions are being deprecated and have been replaced by regular viewController functions. So instead of presentModalViewController and dismissModalViewController, you should put presentViewController and dismissViewController. The modal functions will work for now, but no telling for how long. – Dima May 16 '12 at 19:33
1

You could have 1 modal view controller with 2 views. Then just pick which view you want to display when the view controller loads.

Joel
  • 13,454
  • 5
  • 34
  • 56
  • Hi Joel, that can be a good solution, but I want to be able to dimiss the modal child to show the 1st VC normally. And apart from that the VCs have quite different purposes to mix them in one. Thanks! – Fede Mika May 11 '12 at 19:06
  • You could still "dismiss" the child and show the parent view using UIView animations within the single view controller. However, whether to mix the view controller code bases is a valid objection to this framework. – Joel May 11 '12 at 19:10
0

You should be able to put presentModalViewController anywhere you want, including viewWillAppear.

[self presentModalViewController:myModalViewController animated:NO];

edit: for anyone reading this, see my other (correct) answer, because this one isn't.

Dima
  • 22,982
  • 5
  • 52
  • 82
  • This won't give him the transition animation he is looking for. – Joel May 11 '12 at 19:11
  • Why not? The code I posted was meant to be put into ViewWillAppear of the first modal VC. So if you present the first one and animate it, its child will already be on top of it as it slides in. – Dima May 11 '12 at 19:14
  • Hi lolcat, I already tried this and throws: 'Attempting to begin a modal transition from to while a transition is already in progress. Wait for viewDidAppear/viewDidDisappear to know the current transition has completed' In both ways, animating just the 1st or just the child. Thanks! – Fede Mika May 11 '12 at 19:30
  • Oh interesting, I guess I didn't take into account that one transition would block the other. – Dima May 11 '12 at 19:36
  • And it will crash even if you don't animate any transition at all... (but with no friendly error message) :P – Fede Mika May 11 '12 at 19:48
  • If he animates the first one, you have to wait for it to finish. – Joel May 11 '12 at 19:54