Dismiss pushed view from within Navigation Controller - iphone

I have a Navigation Controller with a View Controller displaying a button. The button is linked to another View Controller using a push segue which automatically adds a top navigation bar with a back button. This all works fine. Pressing the back button slides off the 2nd view and returns to the 1st.
I have a button on the 2nd View Controller, that when pressed runs some code and a delegate call back to the 1st View Controller. Again this works fine.
Now I just need to dismiss the 2nd pushed View from code as if the back button was pressed.
I have tried using dismissModalViewCcontrollerAnimated and dismissViewControllerAnimated, however they both dismiss the whole Navigation Controller which removes view 2 and 1 (returning bak to my main menu).
Whats the correct way to slide off the view.

Obtain a reference to your UINavigationController and call
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
on it.

In Swift it would be calling the method
navigationController?.popViewController(animated: true)

If we use push segue, then use popViewController
#IBAction func backButtonClicked(_ sender: Any) {
self.navigationController?.popViewController(animated: false)
}

In swift you can also call:
self.navigationController?.popToRootViewControllerAnimated(true)

On Objective-C is
[self.navigationController popViewControllerAnimated:YES];
for a jump to the first root controller
[self.navigationController popToRootViewControllerAnimated:YES];
or is a possible move to the specific controller
[self.navigationController popToViewController:(nonnull UIViewController *) animated:(BOOL)];
animation specific animation process of move the controller. If the animation is false the controller will appear without animations.
The UIViewController must be from one which is on the stack.

If NavViewController is used with UIModalPresentationFullScreen then the below line will work
self.navigationController?.dismiss(animated: true, completion: nil)

Related

Getting back to initial viewController from modally presented view

I'am creating a food ordering app. I have four view controller in my Tab bar. I can add food to the cartView (VC2) from my homeView (VC1). These two are in Tab bar. In my cartView(VC2) i have a button to open the orderView modally (this view is not in my Tab Bar.) After a successful order, i have a button in orderView to get back to my homeView (VC1). but because orderView is presented modally I can't use self.navigationController?.popToRootViewController(animated: true). How should I achieve this? thank you.
You should use the dismiss(animated:,completion:) method to dismiss a presented UIViewController instead of poping since the UIViewController wasn't pushed into the navigational stack and cannot be poped.
dismiss(animated: true)
First you need to dismiss presented screen after that you can use notifications for selecting first tab and popToRootViewController or you can define protocol in your orderView and fire it's method in dismiss completation.

How can I go back to the initial view controller in Swift?

So I have a login view, after successful login it goes to the first view of a navigation controller, then the user can go deeper to a settings view and then to a logout view. This logout should take the user back to the login view (which is not part of the navigation controller). It works with this code:
let loginViewController = self.storyboard!.instantiateViewControllerWithIdentifier("Login") as? LoginViewController
self.navigationController!.pushViewController(loginViewController!, animated: true)
But the login view displays the navigation bar at the top, which it shouldn't do, maybe there is something other than self.navigationController!.pushViewController that I should be using?
SWIFT: You should use an unwind segue.
First of all, put the following line in your FirstViewController:
#IBAction func prepareForUnwind(segue: UIStoryboardSegue) {
}
The function actually doesn't have any code inside it.
Now, go to your storyboard and create an unwind segue for LogoutViewController by control-dragging from the yellow button to the Exit button. Like this:
Select the unwind segue created for FirstViewController.
Change the segue identifier:
Go to the code of LogoutViewController and just call the unwind segue normally:
self.performSegueWithIdentifier("unwindToViewController1", sender: self)
Swift 4
self.performSegue(withIdentifier: "unwindToViewController1", sender: self)
If you have a Navigation controller, from your your controller use:
self.navigationController?.popToRootViewControllerAnimated(true)
Look into unwind segueing if you are working with storyboards.
You just need to create unwind option in controller, that you want navigate to:
#IBAction func unwindToMe(segue: UIStoryboardSegue){}
Then create segue from storyboard.
And when you need to navigate back, just call the performSegue method with the unwind segue identifier that you just created.
If you want to do it only from code, than you just can write something like:
let loginViewController = self.storyboard?.instantiateViewControllerWithIdentifier("Login")
UIApplication.sharedApplication().keyWindow?.rootViewController = loginViewController
In this case, you will set your app to initial state.
try it
self.view.window?.rootViewController?.dismissViewControllerAnimated(true, completion: nil)
This will get you back to the beginning of the application flow.
Updated to Swift 4 (thanks #javaBeast)
self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)
self.navigationController?.popToRootViewController(animated: true)
is the best option to go to first controller of navigation controller
and then dismiss the navigation controller
I recommend you to make a segue from one ViewController to another, instead of pushing your ViewController like that.
So first, you need to Ctrl + clic from your first ViewController to your login ViewController, and then in the attribute inspector your give it an Identifier.
Then, all you have to do is this :
self.performSegueWithIdentifier("yourIdentifier", sender: self)
Now, for the navigation bar, I suggest you to remove the navigation controller from the login view, and associate it to your first view. It would remove the navigation bar from your login view.

If "setNavigationBarHidden:YES", then how to come back to previous controller?

I don't want to let the navigation controller show its navigation bar in the whole project.
Now there are three view controllers
(1) Login view controller
(2) Sign up view controller
(3) Home view controller.
I just hope to use action(which can be triggered by any kind of event, i.e. drag gesture, not necessary the pressing button) to switch between these view controllers. But I found once I get to the "signup view controllers", I can not go back to the login view controller, since there is no "BACK" navigation bar.
Questions:
How "PUSH" in one view controller, then "POP" in the other view controller?
Or there is some different way to solve this problem?
Thank you so much, any suggestion is great.
To programmaticaly go backward in a navigation controller's navigation stack, call this method:
[self popViewControllerAnimated:YES];
When and where you call this is up to how you want your app to flow. Essentially, the default navigation controller calls this automatically when the navbar's back button is pressed. But if you hide the navbar and still need to pop back, you can call this method to pop back.
As for pushing forward, it's simply a matter of creating a Push segue on the storyboard, giving it a name, and then in your code, call this method:
[self performSegueWithIdentifier:#"segue_YOUR_SEGUE_ID" sender:self];
On the question of your app, what probably makes most sense is for the login view be a view by itself. It should contain a modal segue to a sign up view for new users as well as a modal segue to the home view controller (which may or may not need to be embedded in a navigation controller).
Performing a modal segue works exactly the same as a push segue (if you're using storyboards. Hook up the segue, choose a modal segue, then call the performSegueWithIdentifier: method in your code when you need the segue to occur.
Dismissing a modal view is slightly different, but still quite simple. It goes like this:
[self dismissViewControllerAnimated:YES completion:nil];
It's fairly check to do with an 'if' statement...
if (self.navigationController.navigationBarHidden == NO) {
//YOUR ACTION
}
Hope that helps!

How can I dismiss a pushViewController in iPhone/iPad?

I need to make a button to dismiss the child view controller from a pushViewController. The action is exactly like the left (back) button on the top navigation bar.
How can I dismiss a pushViewController? Which method should I use ?
Thanks.
UINavigationController's popViewControllerAnimated: method should do it.
In my case where [self performSegueWithIdentifier:#"showAllContacts" sender:self]; pushes a new ViewController, in order to close(dismiss) that view controller which has been pushed I use:
[self.navigationController popViewControllerAnimated:YES];
This is called for a custom button which I added to the navigation bar!

UINavigationController return to first view

I am using a UINavigationController to slide from a UITableViewController to another view. In that second view I have created a custom "Back" button which is attached to an action.
What code do I use to return to dismiss the current view and slide back to my first view? I need something that is the equivalent to [super dismissModalViewControllerAnimated:true]; but obviously this is not a modal view.
Thanks.
Look at one of the following three methods on your navigation controller:
popViewControllerAnimated:
popToRootViewControllerAnimated:
popToViewController:animated:
Depending on how you want to handle it of course. Generally one just uses the first method to go back to the last view (the one that pushed your current view onto the navigation stack).
Use popToRootViewControllerAnimated method in UINavigationController:
[self.navigationController popToRootViewControllerAnimated:animated];