Push from one view controller to another - iphone

I have a tricky situation. The following is the hierarchy of the views that I have.
(Root)Navigation-VC-->View A-->(via Segue push)--> View B-->View C
(Root)Navigation-VC-->View A-->(via modal push)-->(Nav Controller) View D-->View E
There is a cancel button in view D. When I click on it, it correctly shows view A. But For some reasons I would want it to go to View B in the first navigation hierarchy. How can I do such transitions?
If I create a modal segue from view D to view B it destroys the navigation hierarchy that view B is part of. If I push view B from view D then view B becomes a part of a different hierarchy altogether.
Can it be done? Do I have to rethink the design?

Well, after you dismiss the modal view controller, you can always call the method performSegueWithIdentifier:sender: to push view B. Just remember to set the segue identifier in your storyboard.
Call the method dismissViewControllerAnimated:completion: in view A and call the perform segue at the completion block. This is just to avoid any corruption that might happen to the navigation controller if you dismiss and perform segue at the same time.

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];

How to identify the class of a Modal View controller?

In my application, I have to present two modal view controllers one above the other.
Lets say Modal View Controller B is placed over Modal View Controller A. Sometimes, there will be only A and no B.
I want to check from A that whether the top Modal View Controller is B. I know there is a method NSStringFromClass() but I can apply that only if I get the top Modal View Controller.
use (BOOL)[[youObjectInstance isKindOfClass:[ControllerClassYouWantToCheckAgainst class]]
Documentation here:
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/occ/intfm/NSObject/isKindOfClass:

Simulated "Nested" Navigation Controller in iOS

So I have a Navigation Controller in which for every row select, I push ViewController A. However, ViewController A is another table of items.
So I'm having trouble figuring out how to make ViewController A also act as a navigation controller, in which for ever row I select in ViewController A, I can push a view controller, which, we'll name ViewController B.
I tried to add a Navigation Controller object inside an existing Navigation Controller to no avail.
How will I go about this?
Thanks in advance!
NavigationController has a rootController - in your case this is a some kind of table view controller. When you push view controller A - you use parent Navigation Controller. So you can push as many as you need view controllers via this "parent" navigation controller. Read carefully this section of apple docs

Dismissing multiple modal views

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)