views:

1032

answers:

4

I am having trouble getting my modal view controllers to display properly. I have a parent view controller that is the delegate for modal view A. In modal view A I am presenting modal view B, and having the delegate dimiss modal view A.

When modal view B appears it seems to display but the screen dims, and the UI locks up, but the app doesn't crash. I set animation settings to NO and I am still getting the same issue.

Order of events:

  1. Parent View show Modal View A
  2. Modal View A shows Modal View B in Modal View A controller
  3. Parent View dismisses Modal View A in Modal View A controller via delegation
  4. This is where my UI hangs, I can see Modal View B but can't click on it, or do anything
+2  A: 

A modal view controller must have a parent view controller in order to display. If you dismiss the parent view controller ("modal view A", in your case), behavior will be unpredictable.

If you're certain that nested modal view controllers are what you really want, you'll need to dismiss them in reverse order; wait until you're done with "B", then dismiss "B", then dismiss "A".

If you don't need the modal presentation style, you would be better off using a UINavigationController to maintain your stack of view controllers.

Update: here is how I would rearrange your order of events. Presented as code for clarity.

  1. [parentView presentViewController:modalViewControllerA animated:YES]
  2. [modalViewControllerA presentViewController:modalViewControllerB animated:YES]
  3. [modalViewControllerA dismissModalViewControllerAnimated:YES]
  4. [parentView dismissModalViewControllerAnimated:YES]
Tom
I modified my question, and display my order of events. I am dismissing them in reverse order.
Sheehan Alam
Are you dismissing Modal View B before step 3? If not, that order still looks incorrect to me.
Tom
I am not dismissing Modal View B before step 3. I have a cancel and done button in Modal View B that will dismiss itself. I was hoping I could dismiss Modal View A, do some work in Modal View B, and dismiss himself.
Sheehan Alam
That is fine, but you should wait until you dismiss Modal View B to dismiss Modal View A. It will look the same to the user, but won't create this problem.
Tom
In my code, parentView implements the delegate protocol (dismissModal) for Modal View A. If I am in Modal View A, and I call Modal View B, where do I dismiss Modal View A?
Sheehan Alam
If View Controller B has been created by View Controller A, then you can't dismiss A and expect Modal View B to function correctly. You must first finish your work in B, dismiss it, then dismiss A.You should dismiss Modal View A in the ParentView, or Modal View A could dismiss itself once it knows that Modal View B has finished. That may not be the most proper practice, but it works.
Eric Schweichler
@Sheehan: A hackish way to do it in dismissModal with an economy of code would be `if (self.modalViewController.modalViewController) [self.modalViewController dismissModalViewControllerAnimated:NO]; [self dismissModalViewControllerAnimated:NO];`
Tom
@Eric - how can I notify Modal View A that Modal View B has finished?
Sheehan Alam
@Tom - I tried out your code, I get the same results. Modal View B appears, but the screen is locked up.
Sheehan Alam
You might be running into another problem that can occur when you present or dismiss two view controllers in a single iteration of the run loop. Take a look at this question: http://stackoverflow.com/questions/1412021/iphone-crashing-when-presenting-modal-view-controller
Tom
Whats interesting is, when I have my delegate handle presenting Modal View A and Modal View B I get strange errors. The parentView won't display Modal View B but can dismiss Modal View A
Sheehan Alam
@Tom - tried out the link about the run loop. Same results unfortunately.
Sheehan Alam
+1  A: 

Solved by having my parentViewController act as the delegate. Here is my order:

[parentView presentViewController:modalViewControllerA animated:YES]
[parentView dismissModalViewControllerAnimated:YES]
[parentView presentViewController:modalViewControllerB animated:YES]
//Modal B dismisses himself

In my delegate method, I needed to make sure that I dismissed Modal A before presenting Modal B

Sheehan Alam
A: 

Hey Sheehan, may be after long but.. I am in same problem and this is the only post with some answer. I am not getting what you mean by setting delegate of a parentViewController to self is not allowed .

what I am doing right now is

[self presentModalViewController:ViewControllerA animated:YES]; [self dismissModalViewControllerAnimated:YES];// inside ViewControllerA [self presentModalViewController:ViewControllerB animated:YES]; [self dismissModalViewControllerAnimated:YES];// inside ViewControllerB

Problem is after viewControllerA , viewControllerB view is not presenting.

Thanks,

KiranThorat
A: 

I have a main view and need to show a modalview1 where a button present a modalview2. Looks the same you needed. But there is a button in the modalview2 which forwards to the main view.

So the solution is: Main view presents UINavigationController with modalview1 as rootController. Then modalview1 present modalview2 by "[self.navigationController modalview2 animated:YES];".

When modal2 needs to forward to the main view, just make "[self.parentViewController dismissModalViewControllerAnimated:YES];" and UINavigationController is hidden.

Hope it's clear.

slatvick