Dismissing multiple modal views - iphone

My application presents a modal view (A) from the main view that lets the user make a selection. When they make that selection it opens a second modal view (B) on top of the first one (A).
When I'm done with the second modal view (B), and want to dismiss it, I would like to dismiss the first one (A) and the second one (B) at the same time as I no longer need the user to return to that one (A) either.
The only thing I came up with is:
[self.parentViewController.parentViewController.parentViewController. dismissModalViewControllerAnimated:YES];
It works, but it just doesn't look correct. Is this OK to do or is there a more accepted way to do this?

I don't think that your way is wrong. It is what Apple documentation recommends:
If you present several modal view controllers in succession, and thus build a stack of modal view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.

(UIApplication.sharedApplication().delegate! as! AppDelegate).navigationController?.viewControllers.first?.dismissViewControllerAnimated(true, completion: nil)

Related

dismissModalViewController issue in iOs

I have three view controllers, say A, B and C. I am navigating through this views like follows;
A -presenting-> B -presenting-> C -presenting-> B
And from B, if I dismiss I want to navigate to C. But instead of that, now it is moving to A. I can't use dismiss for navigation from C to B (some internal issues). So how can I fix this? Please help.
You hit into a limitation of dismissModalViewController: it will remove all of your modal views (source):
If you present several view controllers in succession, and thus build a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.
What you could do is using a UINavigationController and simply push/pop controllers on to it according to your requirements.
Alternatively, you could simply display the views managed by the various controllers you have by directly calling addSubview on your top view and making sure they cover the whole screen and that the managing controller is correctly retained/released (the view is automatically when you add/remove it to another view).
As a hint, you could do it like this:
where you have presentModal..., use addSubview;
where you have dismiss..., use removeFromSuperview;
store a reference to any view controller whose view you manage like I suggest here in retain/strong property.
Hi use the below code in "C" viewcontroller
[[[self presentingViewController] presentingViewController] dismissModalViewControllerAnimated:YES];

How can I dismiss several controllers ?

Using storyboard , I'm invoking a segue (set type to modal) in order to display the second controller , and the same way to display the third controller. A->B->C. I expect dismiss B and C together , and return to A. There was no navigation view controllers , no popToRootViewControllerAnimated: .
In docs:
If you present several view controllers in succession, thus building a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.
I tried a variety of ways but failed. Am I missing something really simple ?
Try This
[self.presentingViewController.presentingViewController dismissModalViewControllerAnimated:YES]
Also try This
[self.parentViewController.parentViewController dismissModalViewControllerAnimated:YES];

PresentModalViewController, PushViewController, then DismissViewController with a new view shown?

I have a UINavigation controller setup. I was hoping to do this:
From one of the views, I presentModelViewController:animated:, the user selects one of three options, after selecting I want the UINavigationController behind the modal view to change (the user will not see this), then I want to dismissModalViewControllerAnimated to reveal the new view.
Is this possible using the built-in modal view? Or will I need to create a view, add/animate it to the rootViewController so its not in the same stack as the UINavigationController?
Thanks!
A modal view is a view you show modally on top of another view to interrupt the user from the current task. If I understand you correctly, you need two modal views and the selection user make on first modal view will decide what will show as the second modal view. Is that right?
If that's the case, you can make your main view to be the delegate of your first modal view, and send data back to the main view when the user makes a selection, and then main view dismiss the modal view (it's the main view's responsibility to dismiss it). And then based on user's input, you create another view and pop it modally. To make it animate correctly, you need to set the animation of the dismissing of the first modal view to be NO, and then make the animation of populating second modal view to be YES.
Hope this helps.

Calling popToRootViewControllerAnimated from a modal

I have a modal that I am calling presentModalViewController to display from a custom menubar. This menubar is overlaying a viewcontroller that is pushed from a tableView cell.
Now... that all being said, I need to find some way to jump the user back to the root of the tableView from the modal screen. Is this possible? I've been fighting with this for the past couple of hours with no results.
If you're starting from a tablview, drilling down to a detail view via a navigation controller, and then presenting a modal view controller on top of that detail view, you'd have two steps to get back to your list/tableview: First you'd dismiss your modal view controller, then you'd pop your detail view off your navigation stack.
That should put you back where you started (at the tablview), if I'm reading this correctly.
You could pass a reference to the navigation controller to the modal view, and then immediately after calling dismissModalViewControllerAnimated, you can use the reference to the navigationController to pop back to the root view controller.

Is there a delegate in the parent view controller that gets called after a modal view gets dismissed?

After a modal view controller is dismissed, is there any delegate method called to bring the parent view controller to the front?
I ended up using delegation from Apple's View Controller Programming Guide for iOS :
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html#//apple_ref/doc/uid/TP40007457-CH111-SW14
When it comes time to dismiss a modal view controller, the preferred approach is to let the parent view controller do the dismissing. In other words, the same view controller that presented the modal view controller should also take responsibility for dismissing it whenever possible. Although there are several techniques for notifying a parent view controller that it should dismiss its modally presented child, the preferred technique is delegation.
There was a good example in the CoreDataRecepies sample code when adding a recipe that fit what I was trying to do.
i.e., at the "same time" view[Will|Did]Disappear: is being called on the modal view controller as its view is being dismissed, the view[Will|Did]Appear: are sent to the view controller that is being revealed
the code in here should not really need to differ from the reveal code you used when it was first displayed,
if you need data passed back from the modal controller to the one that displayed it, generally the code that dismisses the modal controller lets the other one know
parentController.item = self.chosenItem;
[parentController dismissModal…