Swift Using a Back Button on Two View Controllers - ios

My second view controller is a table view controller. I wish to segue to a first view controller with a back button. I have tried using Editor>Embed In>Navigation Controller. It places a navigation bar on the top where I can place an Item (Back button) on it, and after segueing it back to my first view controller, it becomes very screwy (it shows two navigation bars on top of each other). Is there a better/easier way to move back to my first view controller?

if u want to use navigation controller to manage ur push&pop stuff, just make sure:
navigation controller is ur initial view controller
ur first view controller is the root view controller of navigation controller
set an unique identifier to second view controller
leave ur second view controller alone without any link to other vc
like this
then push like this
let secondVC = (self.storyboard?.instantiateViewControllerWithIdentifier("identifier"))! as! urVC
secondVC.title = "second view controller"
self.navigationController?.pushViewController(secondVC, animated: true)
and pop like this
self.navigationController?.popViewControllerAnimated(true)
alternatively, u can use unwind segue following this tutorial

Related

How to dismiss all but one view controllers and pop to specified view controller in Swift?

I have a bunch of views and then a logout button, which logs the user out and takes them to the first view controller (a login/register screen). I tried doing this with a modal presentation, but it destroys my navigation, and I can't use a pop to root view controller because it is not the root view controller - I am at least 2 navigation controllers deep. How would I go about somehow displaying only the first one? I basically need it to act as if the app was just relaunched. Would unwind segues help in some way? Thanksthis is what I mean by messing up the navigation. The following view controllers now pop up, instead of (the following pic is from the actual first time launching the app) how it should look
Supposing that you use a UINavigationController, you can use the UINavigationController.setViewControllers(_:animated:) method in combination with UINavigationController.viewControllers. The latter provides you an array of view controllers in the exact order they are stacked by the navigation controller, while the former can be used to alter the navigation stack. For instance, if you want to keep only the first 3 view controllers in the navigation stack:
guard let navigationController = self.navigationController else { return }
let viewControllersToKeep = navigationController.viewControllers.prefix(3)
navigationController.setViewControllers(viewControllersToKeep, animated: true)
Note: The animation performed by the navigation controller in this case is usually (I believe always, but not 100% sure) a push animation instead of pop, which might not be the desired one.
So if you want to make the navigation controller to perform a pop animation, then you should call pop until you reach the desired view controller, but the key is to animated a single pop. For the same example as above:
guard let navigationController = self.navigationController else { return }
let numberOfPops = navigationController.viewControllers.count - 3
for i in 0..<numberOfPops {
let animated = i == 0
navigationController.popViewController(animated: animated)
}
Hope this answers the question about popping to the desired view controller (you can detect your view controller by its type, then you can find out its index and use the logic above).
If you want to dismiss to the first presenting view controller, then you need to call UIViewController.dismiss(animated:completion:) on the first presenting view controller. dismiss method will behaves like this:
If the view controller is presenting a view controller, then it will dismiss the presented view controller;
If the view controller is not presenting any view controller, but it is presented by another view controller (i.e. has a parent view controller), then it will ask the parent view controller to dismiss it;
If none of the above, it will do nothing.
So long story short, you need to call dismiss on the view controller that you want to be left on the screen.
I basically need it to act as if the app was just relaunched.
In this case, assigning the login/register screen controller to UIWindow.rootViewController seems to be the right option. This topic has been already covered here: Swift ios set a new root view controller

How to back to reveal view controller after perform segue a view controller in swift?

I use Reveal View Controller in Swift 3. When in my "sw_front" tagged mymainpageViewController I use perform segue a View Controller. When I want to back its nothing working. (perform segue or reveal view controller push controller..) How can I back to mymainpageViewController?
Use this code in your Second View Controller from where you want to go back:
If you want to go back to the previous view controller
self.navigationController?.popViewControllerAnimated(true)
If you want to go back to the root view controller
self.navigationController?.popToRootViewControllerAnimated(true)

Why Do I Need A Second Navigation Controller?

I have the following on my storyboard:
A navigation controller with a table view controller as its root view controller.
The table view controller's navigation item has right bar button (Add) that segues to a 2nd navigation controller.
The 2nd navigation controller has a 2nd table view controller as its root view controller.
The 2nd table view controller's navigation item has a left bar button (Cancel) and a right bar button (Save) both of which unwind back to the 1rst table view controller.
This all works fine: I can navigate from the 1st table view controller to the 2nd and back.
If I remove the 2nd navigation controller and instead segue directly from the 1st table view controller to the 2nd then the 2nd table view controller's navigation item is not displayed. What is going on? Why do I need the 2nd navigation controller?
remove the intermediate navigation controller and connect your second VC to initial vc and change your segue type kind model from present modally to show e.g push
if you are using present then navigation controller is not a manodatry but on your save and cancel button purpose you need to embed with navigation controller, in here you push then no need of Second Navigation Controller.
on navigation use
[self performSegueWithIdentifier: #"sample" sender: self];
on your second by default navigation bar will comes, if you need add barbuttons in right and left side, once press the done button use the following comment
[self.navigationController popToRootViewControllerAnimated:YES];
it automatically come back to initial VC,
you can get the detail of segues in apple
You don't need a second navigation controller. This is what you need to do to after you do a Show segue to the second TableViewController:
Confirm that you're actually doing a Show (e.g., Push) segue. If you have to do a Present Modally segue, then you will need another UINavigationController.
Add a Navigation Item (not a Navigation BAR) to the second TableViewController. You'll then be able to add a title, add navigation buttons, etc.

Adding a Back Button on A Table View Controller

I am interested in implementing a back button to segue to a previous view controller. I have tried to embed a navigation bar onto the the top of the table view controller but the issue is when I do segue back to the first view controller, by default, Xcode keeps the navigation bar with a back button to go back to the table view controller. Is there a simpler way to implement a segue to go back to the first VC without the navigation bar remaining?
I'm not too sure if this works, but embed your view controllers including the first one inside the navigation controller. That would make all your view controllers with navigation bar above.
On the main view controller (the one you do not want to have the navigation bar), add the line of code inside your viewDidLoad method.
Swift 3:
self.navigationController?.navigationBar.isHidden = true
I found an easy way. On your TableViewController, drag a UIview to the top of the view controller and itll let you insert the view. From there just add your back button
Just assign your "Back" UIBarButtonItem to this method and it should work.
#IBAction func backButtonPressed(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
Sounds like a few problems
1) How are you navigating from the first view controller? Is it with Show (Push) if not, you are replacing the first view controller in your stack.
2) Based on your problem, make sure your first view controller is not embedded in a navigation controller
3) As Dylan mentioned, you need to hook your back button to remove the current view controller to return to the first one
4) Extension of (3), how are you segueing back to the first view controller? If you are explicitly navigating to first view controller with your back button handler, it's not actually going back but rather forward and keep the navigation bar.

Navigation controller dont work

I have three view controllers in my storyboard.
In second controller I have navigation controller embeded. Second controller is connected to third with segue.
From first view controller, I call second one by presentViewController. In presented view controller I press a button that trigger a segue. What I expect is to see navigation controller in second and third view controller, but third viewcontroller is also shows from bottom to top with out navigation controller.
How to make nav controller visible in second and third view controller ?
Select the segue
between your secondView and thirdView in your StoryBoard
and look it has this settings:
Take care the identifier is what you want, the key here is the kind type of segue

Resources