简体   繁体   中英

How do I add a Back Button to the Navigation Bar through Storyboard?

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?

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".

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM