Bart
Bart

Reputation: 53

Standard Back Button in XCode (XIB)

I can't get the standard back button of iOS into a navigationBar because I can't find it in the Object Library, so can I do it with code or something else?

I just want the normal, standard, blue back button - you know which I mean.

Upvotes: 1

Views: 4438

Answers (3)

Mark Ebert
Mark Ebert

Reputation: 1

I'm running Xcode 7.2. This was driving me crazy, but I figured it out. Here are all the pieces you need to make the Back button appear (make a test project to prove it):

1) You have to have a Navigation Controller and it has to be set to be the initial view controller. So add the Navigation Controller, you will import two tables. Click on the Navigation Controller and on the properties list, check the box that reads "Is Initial View Controller". You will now see and arrow pointing to this view.

2) In our case we want a ViewController and not the included / connected TableViewController, so delete the TableViewController (RootController) and add a new ViewController.

3) Connect the Navigation Controller to the new ViewController by clicking on the top bar of the Navigation controller and orange circle with the arrow pointing left. Hold the Control button on your keyboard down and click and drag from the orange circle to the ViewController and let go. When given the list of options on how to connect the two views, select 'root view controller'.

Done! Now you the functioning navigation bar and you automatically get the back arrow on all segues added. Test this. Add another ViewController and connect to it with a button on the existing ViewController. Use the Control-click-drag approach from the button to the newest ViewController. Select the 'show' option for the new segue you created.

Run it. You'll see the back option has automatically appeared when you click the button and moved to the newest ViewController.

This is all provided by the Navigation Controller, but only when you make another controller the RootController. Happy navigating!

Upvotes: 0

Aaron
Aaron

Reputation: 7145

To "automatically" have a back button you need first have a UINavigationController. Then you need to take a different UIViewController and add it as the root view controller in UINavigationController's init method:

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:someOtherViewController];

Be sure to also set a title for someOtherViewController, usually in it's viewDidLoad or initializer. I'll tell you why this is important in a second:

self.title = @"Some other VC";

Then take a second UIViewController and push it onto your navigation controller:

[navigationController pushViewController:anotherViewController animated:YES];

You now have two UIViewControllers on your navigation stack: someOtherViewController and anotherViewController.

Your view will now have a back button with "Some other VC" in it. This is the title of the view controller that was just moved out of view:

I would also suggest reading up on how UINavigationControllers work and searching this site a bit more for customizing the back button. There are plenty of threads about it.

Upvotes: 3

BloonsTowerDefence
BloonsTowerDefence

Reputation: 1204

You can't add the back button yourself. The back button is part of the Navigation controller. If you embed a Navigation controller into your view(s), the back button will appear and be populated by the name of the previous view.

If you're using storyboards select your view controller, then in top menu choose "editor" -> "embed in" -> "navigation controller".


Edit: Here is an exmaple.

Upvotes: 1

Related Questions