Xcode / Swift: How I implement a back button? - ios

I just started with Xcode and Swift.
I try to build my first little App for iOS. But now I have the problem, that I don't know how to implement a the back button, so that i come back to the view before.
My Storyboard look like this:
When I open the A-Z view, I want to display the Back Arrow, which turn me back to the Item 2 view.
To open the A - Z view I connect the button "Medikamente A - Z" with the Navigation Controller.

When using storyboards the back button is usually implemented with unwind segue.
I usually like to follow raywenderlich toturials on UI related topics, like this - http://www.raywenderlich.com/113394/storyboards-tutorial-in-ios-9-part-2
It include a detailed example of how to implement back button in storyboards. Quoting from it -
Storyboards provide the ability to ‘go back’ with something called an unwind segue, which you’ll implement next.
There are three main steps:
1. Create an object for the user to select, usually a button.
2. Create an unwind method in the controller that you want to return to.
3. Hook up the method and the object in the storyboard.

When using UINavigationController, whenever you push to a new ViewController the back button will automatically appear, so you can jump back to the previous View Controller.
So it's works like:
UIViewController -> UIViewController -> UIViewController
A back button will appear on the last 2 so you can pop back the the previous ViewController.
You don't have to do any additional coding for the back button to appear, it'll do it on its own. I hope this clears it up. Let me know if you have any questions.

To implement a back button, your root view controller has to be a Navigation Controller.
The first view controller becomes the navigation root of the navigation controller.
If you want to present another view controller, you select a "Show Detail" relationship as the action for the button which should show the view controller. To do this, Ctrl-click and drag from the button to the destination view controller and select "Show Detail".

I had the same problem, even when on the storyboard the back button was visible at design time.
I deleted the segue, and recreated it with "Show" instead of "Show detail". Changing the segue to "Show" had no effect. I think this is a bug, so if you miss that back button delete and recreate the segue.

Related

Swift NavigationControllerBar Back Button

I am new in iOS development and in Swift. I have a question. I added in one of the ViewController NavigationController. But I have some problems with back button cause it doesn't appear on other Views. I tried with self.pushViewController() and with self.present() but it doesn't work. If I mark in NavigationController Is Initial View Controller then everything is fine but I don't want it because then app starts with this screen(where I have NavigationController).
Please help me, what I should to add or to write?
This is an image of my storyboard
And this is what I have if I run and go to another ViewController, as you can see I don't have navigation bar and back button.
You got 2 options :
1) Add a navigation controller in the root ViewController, Hide throughout except the last one. Make sure you push the last VC so Back option will be there by default
self.navigationController.pushToViewController(BarCodeViewController)
2) Use a custom View on top of last viewController add a custom button to that view. But this time present it from previous ViewController
self.present(BarCodeViewController)
when back button clicked dismiss it by adding target to the button. self.dismiss()

Creating a UIButton that pages back to the previous page

I'm sure this is super simple but I'm new to iOS development so I must be missing something.
In xcode I have a like 5 page app and I can navigate forward no problem... To navigate backwards shouldn't I be able to create the button, then simply control and drag to the page I want it to go back to?
Yes. This is called an unwind segue. If using swift, this is how to do it.
Write this code in the view controller file that you want to go back to:
#IBAction func unwindToPreviousScreen(segue: UIStoryboardSegue) {
}
After writing this code, go to the storyboard, and go to view controller that is the one you don't want to go back to, and control-drag from the view controller to the exit icon in the tab bar of the view controller (far right icon). Make sure you have clicked on the view controller.
It doesn't have to be unwindToPreviousScreen, it can be whatever you like, but be sure to keep the unwindTo part intact.
Apologies if you are using a different language, I am just a swift person.

Unwind segue function with Back button

I'm trying to call function when i'm pressing default Back button (e.g. < Settings) in Navigation Controller.
At other situations i'm creating custom button, after i'm Ctrl+Dragging to red Exit button in XCode and pointing it to Selection segue.
Now i need to use default back button of navigation controller, but i can't set assign it to segue action because it's no back button in Storyboard view (but it exists in Simulator).
Simulator:
XCode:
I can create custom button in navigation contoller and assign it so custom unwind segue, but that button will be ugly without "< " symbol.
I've found some answers at stackoverflow, but they all are written in Objective-C :(.
You can use a delegate method to accomplish what you are doing.
Here is a really thought out swift tutorial that deal with delegates and segues:
http://makeapppie.com/2014/07/05/using-delegates-and-segues-part-2-the-pizza-demo-app/
What you are asking, ability to catch the back button's action, isn't possible the way you describe it.
Most people would recommend creating a custom button, then use an unwind segue.
There are lots of tutorials on custom back buttons just in case you're new to swift, and the ugly part of the custom back button can be easily fixed by putting your own arrow there.
Also I found this previous asked question for future reference: Unwind segue from navigation back button in Swift
Actually I just assigned the unwind segue to the entire view controller (right click from the yellow view controller outlet and drag to the exit outlet then select the function). And it works great so far. No need custom button, no delegate.
In fact, in my project the child is supposed to call the different parent's function according to current caller view controller and simply using the same function name made my dreams come true.
This page helped me a lot: http://spin.atomicobject.com/2014/12/01/program-ios-unwind-segue/

bar button item segue not adding navigation bar at top of next view

When I drag to the next view controller to add a segue the new view controller has no navigation bar with a back button. I thought that was the default when you add a segue to a new VC. I'm also not able to drag a navigation bar to the top of the table view controller. Am I doing something wrong? Thanks.
Maybe the problem is the kind of segue that you are you using. The Segue type should be "Show (e.g Push)". Click on the segue and in the right panel go to the "attributes inspector" (the 4th one). And here you can check the type.
A view controller like in your case only shows a navigation bar by default, when the view controller itself or one of its parents (connected by a push-segue) is embedded in a navigation controller. Obviously, the navigation controller must not have hidden the navigation bar.
So, you either forgot to put a navigation controller into play or your segue is modal and not push.
Well, it's two years since you asked your question, but when it happened to me, I realized it was because at one point, I had accidentally created my segue as a different kind of segue (a detail one, I think), but changing it back to a "Show" segue didn't actually change it's behavior. When I found the segue in my storyboard xml, it was listed as a "Show" segue, but with a different custom action that suggested it was going to do something completely different.
Anyhoo, deleting and recreating the segue in Xcode fixed it. I probably could have also deleted that custom action in the xml file, too.

add back button to first screen of navigation controller in iOS

I know that a push segue automatically adds a back button so that the presentee can return to the presenter on click. What I want is for the back button to be available on the first UIViewController of the NavigationController (basically the only view controller in the chain that does not have a back button). How do I force add the back button?
I cannot simply add a custom image because I want the exact same chevron that comes with the iOS back button.
The easiest (yet hackish) way to achieve this is probably adding a dummy view controller in the stack before the actual first one, so you will get a proper back button. Use dummy's backBarButtonItem to customize the button title if you need, and -viewWillAppear: to respond to the button being tapped.
What I want is for the back button to be available on the first UIViewController .... How do I force add the back button
This whole thinking and logic is wrong. This is not how iOS UINavigationControllers work. You have a starting page (mainViewController) in a UINavigationControllers and from there you "move on" to something else. But you always come back to the main view controller. Just think about it, if you put a back button on the main view controller, where will the user go back to?
If you have a situation like this.
SomeViewController --> UINavigationController --> MainViewController
Once user is on SomeViewController and you take him to MainViewController which is part of UINavigationController then back button makes sense.
Just a FYI - you can easily drag and drop a UIBarButtonItem to any UINavigationController in Xcode and call it "Back". You will have to then connect that back button to another view controller.
Maybe it doesn't make sense but why can't you add a custom image? If all you want is "Back" chevron, can't you take a screenshot of this button and then put this image on your custom UIBarButtonItem?

Resources