I have 3 navigation controllers. Each with many view controllers.
1 NavigationController (modal Segue)-> 2 NavigationController (model Segue)-> 3 NavigationController
Now, how do you go from #3 NavigationController back to #1 NavigationController that I have been before? So I want
1 NavigationController (modal Segue)-> 2 NavigationController (model Segue)-> 3 NavigationController (HOW???)-> 1 NavigationController
(To clarify, I would not want to go to a new 1 NavigationController. I want to go to the one that I used before.)
Help!
[[self navigationController] popViewControllerAnimated:YES];
If you just want to dismiss the whole stack of 3 NavigationController, you can call this within any view controller in the 3
Objective C
[self.navigationController dismissViewControllerAnimated:YES completion:nil]
Swift 3, 4
self.navigationController?.dismiss(animated: true)
This will bring you back to the status before (model Segue)-> 3 NavigationController.
Maybe you can somehow call this in 2 before calling this in 3?
Use that:
[self.**presentingViewController** dismissViewControllerAnimated:YES completion:nil];
instead of:
[self dismissViewControllerAnimated:YES completion:nil];
In a view controller in navigationController1's stack, create an unwind #IBAction method:
Swift
#IBAction func unwindToMyViewController(_ segue: UIStoryboardSegue)
Objective-C
- (IBAction)unwindToMyViewController:(UIStoryboardSegue *)segue
In your storyboard, you can then hook up an unwind segue from a button in a view controller that is in the stack of navigationController3, by dragging from the button to the exit icon…
from there, select the unwind segue created above. When triggered, the segue will unwind ALL view controllers back to the view controller containing the unwind segue.
With help of this, You can get the 1 Nav controll :-
[(UINavigationController *)self.view.window.rootViewController popViewControllerAnimated:YES];
this code will pop a navigation controller with all view controllers in it
// pop root view controller
UIViewController *rootViewController = [self.navigationController viewControllers][0];
[rootViewController dismissViewControllerAnimated:YES completion:nil];
so you can do something like this:
// pop navigationController3 without animation
UIViewController *rootViewController3 = [navigationController3 viewControllers][0];
[rootViewController3 dismissViewControllerAnimated:NO completion:nil];
// pop navigationController2 with animation
UIViewController *rootViewController2 = [navigationController2 viewControllers][0];
[rootViewController2 dismissViewControllerAnimated:YES completion:nil];
Related
I have a firstViewController embedded in a firstNavigationController that has a modal segue to a new secondViewController embedded in another secondNavigationController. This new controller performs an unwind segue back to the firstViewController
secondViewController:
- (void) cancelAction{
[self performSegueWithIdentifier:#"backHoney" sender:self];
}
firstViewController:
- (IBAction)backToHoney:(UIStoryboardSegue *)sender{
}
This works but there is a case when another viewC embedded in a Nav has a modal segue to the firstViewController. When the user goes from this viewC to the firstViewController then to the secondViewController and tries to unwind back to the first, it unwinds all the way back to viewC instead of the first.
you should use popToviewController instead of unwing segue. you can go to any viewController of navigation stack by this.
for example,
NSArray *viewArr = [self.navigationController viewControllers]; //returs viewcontroller array
[self.navigationController popToViewController:[viewArr objectAtIndex:0] animated:YES];
//you can pass different index to go to differen VC
You can refer my this answer for more detail.
hope this will help :)
I would like to have my application have custom buttons in the CameraViewController; one pushes to Rustles ViewController (Top VC in photo below) and the other transitions to ViewController (Bottom VC).
Right now my application can properly segue to the viewControllers by using the following method:
-(void)segueToRustlesTableViewController{
if (debug==1) {NSLog(#"Running %# '%#'", self.class, NSStringFromSelector(_cmd));} // simple debug statement, can ignore
// Instantiate nav controller which segues to table view
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil]; // must assume only IPhone
RustlesTableViewController *rustlesTVC = (RustlesTableViewController *)[storyboard instantiateViewControllerWithIdentifier:#"RustlesView"];
[self performSegueWithIdentifier:#"tableSegue" sender:self];
[self.PickerController presentViewController:rustlesTVC animated:NO completion:nil];
}
However when I get to the UIViewControllers I can't transition back to the old UIViewControllers, presumably because I have no Navigation Controllers embedded in each UIViewControllers.
My code right now wouldn't work for a NavigationController but I don't really know how to transition to the NavigationController and then the UIViewControllers after the Navigation Controller.
How do I transition from CameraViewController to aNavigationController and then to the RustlesViewController?
If you aren't making use of a navigation controller, I would make a custom button in the spawned view controller (Rustles, View) and call
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
I believe this should work.
I'm building a complex app that has kind of a branch in the middle.
At some point in the app, a particular UIViewController is presented, we'll call it mainViewController (shortened mainVC).
The mainVC presents another view controller, by code, using the following code (I strip out parts of it for privacy reasons):
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"SecondaryStoryboard" bundle:secondaryBundle];
SecondViewController *secondVC = [storyboard instantiateInitialViewController];
[self presentViewController:secondVC animated:YES completion:nil];
So the secondVC will later present another view controller, called thirdVC. This is done using a custom segue, set in the storyboard used in the code above, which code looks like this:
#implementation VCCustomPushSegue
- (void)perform {
UIView *sourceView = ((UIViewController *)self.sourceViewController).view;
UIView *destinationView = ((UIViewController *)self.destinationViewController).view;
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
destinationView.center = CGPointMake(sourceView.center.x + sourceView.frame.size.width, destinationView.center.y);
[window insertSubview:destinationView aboveSubview:sourceView];
[UIView animateWithDuration:0.4
animations:^{
destinationView.center = CGPointMake(sourceView.center.x, destinationView.center.y);
sourceView.center = CGPointMake(0 - sourceView.center.x, destinationView.center.y);
}
completion:^(BOOL finished){
[self.sourceViewController presentViewController:self.destinationViewController animated:NO completion:nil];
}];
}
#end
As you can see this segue presents the destination view controller modally (by the use of presentViewController:) with a custom animation (a slide from right to left).
So basically up to here everything is fine. I present the secondVC with a classic modal animation (slide up from bottom) and present the thirdVC with my custom transition.
But when I want to dismiss the thirdVC, what I want is to go back directly to the mainVC. So I call the following from the thirdVC :
self.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:_animate completion:nil];
That way, I'm calling dismissViewControllerAnimated: directly on mainVC (referenced by self.presentingViewController.presentingViewController), and I'm expecting the thirdVC to be dismissed with an animation, and the secondVC to just disappear without animation.
As Apple says in the UIViewController Class Documentation:
The presenting view controller is responsible for dismissing the view
controller it presented. If you call this method on the presented view
controller itself, it automatically forwards the message to the
presenting view controller.
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.
The issue is that it's not what happens. In my scenario, the thirdVC disappears, and shows the secondVC being dismissed with the classic modal slide to bottom animation.
What am I doing wrong ?
Edit :
So #codeFi's answer is probably working in a classic project, but the problem here is that I'm working on a framework. So mainVC would be in a client app, and the secondVC and thirdVC are in my framework, in a separate storyboard. I don't have access to mainVC in any other way than a reference to it in my code, so unwind segues are unfortunately not an option here.
I've been having this exact same issue, and I've managed to visually work around it by adding a snapshot of the screen as a subview to secondVC.view, like so:
if (self.presentedViewController.presentedViewController) {
[self.presentedViewController.view addSubview:[[UIScreen mainScreen] snapshotViewAfterScreenUpdates:NO]];
}
[self dismissViewControllerAnimated:YES completion:nil];
Not pretty, but it seems to be working.
NOTE: if your secondVC has a navigation bar, you will need to hide the navigation bar in between snapshotting the screen and adding the snapshot as a subview to secondVC, as otherwise the snapshot will appear below the navigation bar, thus seemingly displaying a double navigation bar during the dismissal animation. Code:
if (self.presentedViewController.presentedViewController) {
UIView *snapshot = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:NO];
[self.presentedViewController.navigationController setNavigationBarHidden:YES animated:NO];
[self.presentedViewController.view addSubview:snapshot];
}
[self dismissViewControllerAnimated:YES completion:nil];
I had the same issue and I've fixed it by using UnwindSegues.
Basically, all you have to do is add an IBAction Unwind Segue method in the ViewController that you want to segue to and then connect in IB the Exit action to your Unwind Segue method.
Example:
Let's say you have three ViewControllers (VC1, VC2, VC3) and you want to go from VC3 to VC1.
Step 1
Add a method to VC1 like the following:
- (IBAction)unwindToVC1:(UIStoryboardSegue*)sender
{
}
Step 2
Go in Interface Builder to VC3 and select it. Then CTRL-drag from your VC icon to Exit icon and select the method you've just added in VC1.
Step 3
While still in IB and with VC3 selected, select your Unwind Segue and in the Attributes Inspector add a Segue Identifier.
Step 4
Go to VC3 where you need to perform your segue (or dismiss the VC) and add the following:
[self performSegueWithIdentifier:#"VC1Segue" sender:self];
I got first ViewController with out navigation controller, I go from it to second ViewController via
if (!self.mapViewController)
{
self.mapViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"MapViewController"];
}
[self presentViewController:self.mapViewController animated:YES completion:nil];
I send some data with prepareForSegue to it, but I need that it also be with navigation controller.
I embed in a navigation controller in storyboard but my code still called second ViewController with out navigation.
I am not a professional with the way of the storyboard, however i believe that instead of presentViewController
you should be using the following function to present a storyboard VC based on segues.
[self performSegueWithIdentifier:#"SegueIdentifierHere" sender:nil];
Make sure that in your storyboard you have incorporated a UINavigationControllerVC as well.
Give an identifier to the navigation controller in storyboard. Instantiate and present that.
UINavigationController *navVC = [self.storyboard
instantiateViewControllerWithIdentifier:#"TheNavVCWhoseRootIsMyMapViewController"];
self.mapViewController = navVC.viewControllers[0]; // your map vc is at the root
[self presentViewController:navVC animated:YES completion:nil];
You are using presentViewController:animated:completion which will indeed display a UIViewController without the UINavigationController the previous UIViewController was embedded in.
Try using the following:
[self.navigationController pushViewController:self.mapViewController animated:YES]
From Home view - my RootViewController - I open up 2 ViewControllers one after another as user progresses in navigation hierarchy like so:
1) SecondViewController is pushed by button connected in my Storyboard
2) ThirdViewController is presented modally
[self performSegueWithIdentifier:#"NextViewController" sender:nil];
So, the picture is: RootViewController -> SecondViewController -> ThirdViewController
Now in my ThirdViewController I want to have a button to go back 2 times to my RootViewController, i.e. go home. But this does not work:
[self.navigationController popToRootViewControllerAnimated:YES];
Only this guy goes back once to SecondViewController
[self.navigationController popViewControllerAnimated:YES];
How can I remove both modal and pushed view controllers at the same time?
I had a similar situation, where I had a number of view controllers pushed onto the navigation controller stack, and then the last view was presented modally. On the modal screen, I have a Cancel button that goes back to the root view controller.
In the modal view controller, I have an action that is triggered when the Cancel button is tapped:
- (IBAction)cancel:(id)sender
{
[self.delegate modalViewControllerDidCancel];
}
In the header of this modal view controller, I declare a protocol:
#protocol ModalViewControllerDelegate
- (void)modalViewControllerDidCancel;
#end
And then the last view controller in the navigation stack (the one that presented the modal view) should implement the ModalViewControllerDelegate protocol:
- (void)modalViewControllerDidCancel
{
[self dismissViewControllerAnimated:NO completion:nil];
[self.navigationController popToRootViewControllerAnimated:YES];
}
This method above is the important part. It gets the presenting view controller to dismiss the modal view, and then it pops back to the root view controller. Note that I pass NO to dismissViewControllerAnimated: and YES to popToRootViewControllerAnimated: to get a smoother animation from modal view to root view.
I had the same requirement but was using custom segues between the view controllers. I came across with the concept of "Unwind Segue" which I think came with iOS6. If you are targeting iOS6 and above these links might help:
What are Unwind segues for and how do you use them?
http://chrisrisner.com/Unwinding-with-iOS-and-Storyboards
Thanks.
Assuming your AppDelegate is called AppDelegate, then you can do the following which will reset the rootviewcontroller for the app window as the view RootViewController
AppDelegate *appDel = (AppDelegate*)[[UIApplication sharedApplication] delegate];
RootViewController *rootView = [[RootViewController alloc] init];
[appDel.window setRootViewController:rootView];