I have three view controllers. MainViewController, secondViewController, thirdViewController. I would like to know once I click on done button on the thirdViewcontroller,
How could I go directly yo MainViewController instead of going back secondViewController and then MainViewcontroller by using pushViewController?
Assuming that your using a navigation controller, you can pop to root by calling:
[[self navigationController] popToRootViewControllerAnimated:YES];
This will bring you back to your MainViewController.
Related
i am new to IOS .
My question is, i have some view-controllers as NavigationController,mainVC, VC1, VC2, VC3, CameraVC. In cameraVC i have a done button having action doneClicked. These all View-Controller are pushed in NavigationController. VC1 is presented, not pushed in nav-controller. doneClicked function implemented poptorootviewcontroller. when i click on done button it let me to VC1 but not the mainVC. Is there any way so i can pop all view controller to VC1 and after this automatically dismiss VC1 to mainVC.
Make your MainVC as root view controller and in the IBAction of done button use the code to pop to MainVC.
[self.navigationController popToRootViewControllerAnimated:YES];
Hope it helps.
To pop view controllers
[self.navigationController popToRootViewControllerAnimated:YES];
and to dismiss presented view controller
[self dismissViewControllerAnimated:NO completion:nil]
[self.navigationController setViewControllers:#[mainVC]];
I think this code will work on your situation. iOS Developer Library:
Replaces the view controllers currently managed by the navigation controller with the specified items.
- (void)setViewControllers:(NSArray *)viewControllers
animated:(BOOL)animated
source
you must do this after dissmiss present view controller. Use delegate
Now let's think your navigation stack is empty and your root is mainVC. you want to present VC1 that's ok just present it. But you should give a delegate to mainVC to what's gonna happen after dissmissing VC1. For example you present VC1 from mainVC. And you want to push VC2 after dissmiss VC1. That's ok just in mainVC has a delegate so in this method
[self.navigationController pushViewController:VC2];
Present views does not affect your navigation stack. It's not in your stack. so everytime you dismiss it from a controller you should give an delegate to that controller to what will happen after dissmissing.
Try it.
Hope it helps.
In a UITabBar based application, UIImagePickerController is opened ,after dismissing it UIViewController loads earlier than UITabBar and as a result whole content of UIViewController is not being shown?
I guess after dismissing controller method for navigation would be an issue.
Check if you use below method:
[self.navigationController popToRootViewControllerAnimated:YES];
Then it will navigate flow to root controller.
So replace this method with
[self.navigationController popViewControllerAnimated:YES];
Above method pop back to previous controller.
I am developing an app that as first viewcontroller has an UIViewController.
This controller pushes a NavigationViewController that contains other controllers.
Now I need to pop the RootViewController of the NavigationController to go back to the initial UIViewController.
I tried with
[self dismissViewControllerAnimated:YES completion:nil];
and the app crashes("Tread1:EXC_BAD_ACCESS(code=1, address= ......)").
I tried with
[self.navigationController popViewControllerAnimated:YES];
and nothing happens.
The initial UIViewController calls
[self performSegueWithIdentifier:#"MyIdentifier" sender:self];
In the UIBuilder the segue is of type "Show(e.g. Push)"
Then I have a NavigationViewcontroller that contains the RootViewController and another Viewcontroller.
What I am trying to achieve is to go back to the first viewcontroller (the one outside the navigationcontroller) from the RootViewController. So I should have the navigationcontroller there.
What am I missing?
Apparently I had a couple of GestureRecognizer still in place that were causing the crash of the app.
The right method was:
[self dismissViewControllerAnimated:YES completion:nil];
I'm using a UINavigationController and I have a side menu in it. The problem is that when I want to push a UIViewController in the NavigationController from the side view it doesn't work. To do it, is use that code:
[self.slidingViewController resetTopView];
CDRecentChats* recentchats = [[CDRecentChats alloc]init];
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"conversation"];
[recentchats.navigationController pushViewController:controller animated:YES];
As you can see, I first close de side menu and then call the NavigationController to push a new ViewController. Using that code, the only thing that happens is that the menu is closed, but the NavigationController doesnnt push any ViewController. what can I do?
popToViewController means to go back to one that has already been made part of the navigation controller's stack. If you're trying to show a new one, use pushViewController.
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];