简体   繁体   English

如何通过Storyboard向导航栏添加后退按钮?

[英]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. 在我的MainViewController中,我有performSegueWithIdentifier(...)然后在另一端,我希望导航栏在左侧有后退按钮。 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. 我尝试过使用UINavigationController,每当我将故事板中的“导航项目”拖到导航栏时,我的标题就会被替换,但仍然没有后退按钮。

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." 正如您在问题中所述: “我尝试过使用UINavigationController,每当我将故事板中的'导航项目'拖到导航栏时,我的标题就会被替换,但仍然没有后退按钮。”

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. 您现在将看到2个视图控制器 - 一个是导航控制器,第二个是原始视图控制器。 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. 您会注意到导航控制器现在是应用程序的初始视图,并且将有一条连接segue的线,表示视图控制器是导航控制器的根视图控制器。 Then add another UIViewController (by dragging and dropping) next to the original View Controller. 然后在原始View Controller旁边添加另一个UIViewController(通过拖放)。 Now you'll have 3 View Controller, the Navigation Controller, the original View Controller, and the new View Controller. 现在,您将拥有3个视图控制器,导航控制器,原始视图控制器和新的视图控制器。 Add a UIButton on the upper left of the original view controller. 在原始视图控制器的左上角添加UIButton。 Then Control drag from the button to the new view controller and select the show segue. 然后控制从按钮拖动到新视图控制器并选择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. 根据评论,目标是在用户登录时直接进入MainViewController,但如果用户未登录则转到LogInViewController。

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. 通过转到大纲,选择segue,转到身份检查器并为其指定标识符,将来自LogInViewController的push segue提供给MainViewController标识符。 In ViewDidAppear of the LogInViewController check if the user is logged in. If so, then perform the segue. 在LogInViewController的ViewDidAppear中检查用户是否已登录。如果是,则执行segue。 This will give a back button on the MainViewController to the LogInView Controller. 这将在MainViewController上为LogInView控制器提供一个后退按钮。

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. 替代方案:我将LogInViewController作为应用程序的初始View Controller,但不要将其嵌入到UINavigationController中。 Then I add the MainViewController to the storyboard and embed it in the Navigation Controller. 然后我将MainViewController添加到故事板并将其嵌入导航控制器中。 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. 我建议将它嵌入导航控制器,以防你想要显示其他视图并有一个MainViewController的后退按钮。 Add a UIButton to the LogInViewController and control drag now from it to the Navigation Controller. 将UIButton添加到LogInViewController并控制现在从它拖动到导航控制器。 Select the present modally segue. 选择当前的模态segue。 Select the segue in the outline of the storyboard, and in the attributes inspector, give it a name like "present MainViewController". 在故事板的轮廓中选择segue,然后在属性检查器中为其命名,如“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]; 在LogInViewController的ViewDidAppear方法中,检查用户是否已登录,如果是,则调用[self performSegueWithIdentifier:@"present MainViewController" sender:nil]; This will present the MainViewController if the user is logged in but not otherwise. 如果用户已登录,则会显示MainViewController,但不会显示。 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. MainViewController中没有后退按钮,因为它是以模态方式呈现的导航控制器的根视图控制器。 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. 如果您希望左上角按钮是一个将用户注销的按钮,则可以将UIBarButton添加到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. 现在转到LogInViewController代码并添加- (IBAction) nameOfUnwindSegue : (UIStoryboardSegue *) segue{}返回故事板并控制从此栏按钮拖动到故事板中View Controller顶部的退出图标。 Select nameOfUnwindSegue (you can name this whatever you want). 选择nameOfUnwindSegue(您可以根据需要命名)。 This creates an unwind segue which will dismiss the view controller. 这会创建一个解除视图控制器的展开segue。 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. 您可以通过在storyboard中单击它来获取标识符,转到属性检查器,并为其指定标识符“在MainViewController中的prepareForSegue方法中,您可以检查segue.identifier是否等于标识符,如果是,请调用注销方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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