4

I'm very perplexed right now. Everywhere online I am seeing tutorials to add a custom back button, but I can't even seem to get the default one going.

In my MainViewController, I have performSegueWithIdentifier(...) and then on the other end, I want the Navigation Bar to have the back button on the left. How does one achieve this? I can't seem to find any tutorials online. I've tried having a UINavigationController, and whenever I drag the "Navigation Item" in the storyboard up to the Navigation Bar, my title gets replaced and there still isn't a back button.

How do I add the default back button?

David
  • 7,028
  • 10
  • 48
  • 95
  • You should be able to edit its title in the Inspector menu while selecting a `UINavigationItem`. Otherwise, if you've properly embedded your view in a `UINavigationController`, the back button should be generated automatically. – AstroCB Jan 26 '15 at 03:38
  • The UINavigationItem changes my title and there doesn't seem to be a way to select the kind of button in any inspector menu. I'm not sure how to properly embed a UINavigationController – David Jan 26 '15 at 03:41
  • You don't see [this](http://i.stack.imgur.com/5GyGH.png) in the menu? – AstroCB Jan 26 '15 at 03:42
  • I do, but nothing I write in the back button piece works. – David Jan 26 '15 at 03:44
  • The back button appears by default when you push a new view controller onto the navigation controller (via code or a segue). Show us a picture of your storyboard so we can see what you're doing plus any code that you're using to push a new view controller (none should be necessary in a minimal example). – Robotic Cat Jan 26 '15 at 03:53
  • If a UINavigation controller has been embedded in a viewcontroller on your storyboard and you perform push segue from that view controller to another, the destination view controller automatically gets the navigation bar/controller added to it, along with the default behavior for push/popping views -- which includes the back button. can you screen shot what your storyboard looks like? – Louis Tur Jan 26 '15 at 03:56
  • Oh, I understand your question now... One sec... I'll write an answer. – Lyndsey Scott Jan 26 '15 at 04:07
  • @David Have you tried the suggestion in my answer? I get the feeling people aren't understanding your original question... Adding a "Navigation Item" will in fact reset the title in the way you've observed. You need to be using a "Bar Button Item" to set the back button in your interface. – Lyndsey Scott Jan 26 '15 at 04:22
  • @LyndseyScott Do I still need to use the UINavigationController if I go that route? How would I have the Bar Button Item exhibit the functionality of a usual Back button? I don't mind it resetting the title if I could just use that title attribute instead. – David Jan 26 '15 at 04:27
  • @David You don't need to use a UINavigationController in that case. Just link up the Bar Button Item as an IBAction like you would with any other button and have the IBAction method perform the view's dismissal. – Lyndsey Scott Jan 26 '15 at 04:30

2 Answers2

9

As you state in your question: "I've tried having a UINavigationController, and whenever I drag the 'Navigation Item' in the storyboard up to the Navigation Bar, my title gets replaced and there still isn't a back button."

If you drag a "Navigation Item" onto a nav bar, you'll get exactly the behavior you've described -- your "Navigation Item" will serve as a title, not as a button. To add a custom back button to your nav bar in your storyboard, instead of adding a "Navigation Item" to your nav bar, you need to add a "Bar Button Item".

Lyndsey Scott
  • 37,080
  • 10
  • 92
  • 128
4

The default back button is... default :). I'll give you a quick example. Create a new project with a single view. Go into story board, click on the single view controller, then go to Editor in the menu -> Embed in -> Navigation Controller. You'll now see 2 view controllers - one being the navigation controller, the second being the original view controller. You'll notice that the Navigation Controller is now the initial view of the app, and there will be a line connecting the segues, indicating that the view controller is the root view controller of the navigation controller. Then add another UIViewController (by dragging and dropping) next to the original View Controller. Now you'll have 3 View Controller, the Navigation Controller, the original View Controller, and the new View Controller. Add a UIButton on the upper left of the original view controller. Then Control drag from the button to the new view controller and select the show segue. If you now try running the app in simulator, you'll see the original view controller first. Tap the button and you'll see the new view controller with a Back button! You'll get the back button without even trying -- because it is default.

Edit

Based on the comments, the goal is to go straight to the MainViewController if the user is logged in but go to the LogInViewController if the user is not logged in.

Give the push segue from the LogInViewController to the MainViewController an identifier by going to the outline, selecting the segue, go to the identity inspector, and give it an identifier. In ViewDidAppear of the LogInViewController check if the user is logged in. If so, then perform the segue. This will give a back button on the MainViewController to the LogInView Controller.

In my app, I do things slightly differently.

Alternative: I make the LogInViewController the initial View Controller of the app, but do not embed it in a UINavigationController. Then I add the MainViewController to the storyboard and embed it in the Navigation Controller. I am suggesting to embed it in the Navigation Controller in case you have other views you want to show and have a back button to the MainViewController. Add a UIButton to the LogInViewController and control drag now from it to the Navigation Controller. Select the present modally segue. Select the segue in the outline of the storyboard, and in the attributes inspector, give it a name like "present MainViewController". In the ViewDidAppear method of the LogInViewController, check if the user is logged in, and if so call [self performSegueWithIdentifier:@"present MainViewController" sender:nil]; This will present the MainViewController if the user is logged in but not otherwise. There will not be a back button in the MainViewController because it is the root view controller of the Navigation Controller that was presented modally. If you want the upper left button to be a button that logs the user out, you can add a UIBarButton to the left side of the MainViewController. Now go to LogInViewController code and add - (IBAction) nameOfUnwindSegue : (UIStoryboardSegue *) segue{} Go back to the Storyboard and control drag from this bar button to the exit icon on the top of the View Controller in storyboard. Select nameOfUnwindSegue (you can name this whatever you want). This creates an unwind segue which will dismiss the view controller. You can given an identifier by clicking on it in storyboard, going to the attributes inspector, and give it the identifier " In the prepareForSegue method in the MainViewController, you can check if segue.identifier is equal to the identifier and if so, call a log out method.

Josh Gafni
  • 2,831
  • 2
  • 19
  • 32
  • So, I have a LoginRegisterViewController that Segues into a MainViewController. I want that to be the RootViewController for my NavigationController. From there, I segue into the controller I want the back button in. In storyboard, how would I properly hook up the Navigation Controller View? – David Jan 26 '15 at 04:12
  • Exactly as I described above; however, you have to tell storyboard that what I was calling the "original" and "new" UIViewControllers are not just UIViewControllers, but a subclass of UIViewController. Assuming you already have LoginRegisterViewController and MainViewController files made that inherit from UIViewController, all you have to do is click on the original view controller, click on the identity inspector, and instead of class ViewController, change it to LoginRegisterViewController. Click the new view controller and do the same with MainViewController. – Josh Gafni Jan 26 '15 at 04:16
  • I don't want LoginRegisterViewController to be part of the stack though. Is there a way to just start it from MainViewController. – David Jan 26 '15 at 04:19
  • Is your goal to basically go straight into the MainViewController if the user is already logged in and also have a way of logging out and returning to the LogINViewController? I'm asking because it is a little jarring for the user to be shown a back button without ever having seen the screen. But I am assuming they have logged in at some point? – Josh Gafni Jan 26 '15 at 04:22
  • If the user isn't logged in, they get the LoginRegisterViewController. If they are logged in, I bypass the LoginRegisterViewController and go straight to the MainViewController. I don't want the MainViewController to have the back button. In fact, this is probably the only ViewController that needs the back button anyways, so maybe the UINavigationController is overkill, but do ViewControllers still stack even when there isn't a UINavigationController? – David Jan 26 '15 at 04:25
  • Thank you! I'm very new to iOS development x.X – David Jan 26 '15 at 04:27
  • No worries! Btw... I highly recommend CS 193p that is free on iTunes U. It is a Stanford course in iOS development. The latest course (just starting) is in Swift, but you can always take the previous one with Objective-C. It walks through the segues and what not. – Josh Gafni Jan 26 '15 at 04:51
  • Btw.. if you find the answer helpful, could you accept it? :) – Josh Gafni Jan 26 '15 at 04:51
  • The OP's original question stated: "I've tried having a UINavigationController, and whenever I drag the 'Navigation Item' in the storyboard up to the Navigation Bar, my title gets replaced and there still isn't a back button." As I say in my answer and as you would observe if you try it on your own in the storyboard, if you drag a "Navigation Item" onto a nav bar, it'll serve as a title, *not* a button. The OP should be dragging a "Bar Button Item" onto his nav bar instead to create a button... I think you've complicated the question. – Lyndsey Scott Jan 26 '15 at 22:43