如何通过 Storyboard 向导航栏添加后退按钮? [英] How do I add a Back Button to the Navigation Bar through Storyboard?

查看:42
本文介绍了如何通过 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.

在我的 MainViewController 中,我有 performSegueWithIdentifier(...) 然后在另一端,我希望导航栏在左侧有后退按钮.如何实现这一目标?我似乎无法在网上找到任何教程.我试过使用 UINavigationController,每当我将故事板中的导航项"拖到导​​航栏时,我的标题都会被替换,但仍然没有后退按钮.

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?

推荐答案

默认后退按钮是...默认 :).我会给你一个简单的例子.创建一个具有单一视图的新项目.进入故事板,单击单个视图控制器,然后转到菜单中的编辑器 -> 嵌入 -> 导航控制器.您现在将看到 2 个视图控制器 - 一个是导航控制器,第二个是原始视图控制器.你会注意到 Navigation Controller 现在是应用程序的初始视图,并且会有一条线连接 segues,表明视图控制器是导航控制器的根视图控制器.然后在原始视图控制器旁边添加另一个 UIViewController(通过拖放).现在您将拥有 3 个视图控制器、导航控制器、原始视图控制器和新的视图控制器.在原视图控制器的左上角添加一个 UIButton.然后 Control 从按钮拖动到新的视图控制器并选择 show segue.如果您现在尝试在模拟器中运行该应用程序,您将首先看到原始视图控制器.点击按钮,您将看到带有后退按钮的新视图控制器!您无需尝试即可获得后退按钮 - 因为它是默认设置.

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.

编辑

根据评论,如果用户已登录,则目标是直接转到 MainViewController,如果用户未登录,则转到 LogInViewController.

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.

通过转至大纲、选择转场、转至身份检查器并为其提供标识符,为从 LogInViewController 到 MainViewController 的推送转场提供一个标识符.在 LogInViewController 的 ViewDidAppear 中检查用户是否已登录.如果是,则执行 segue.这将在 MainViewController 上为 LogInView 控制器提供一个后退按钮.

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.

替代方案:我将 LogInViewController 设为应用程序的初始视图控制器,但不将其嵌入 UINavigationController.然后我将 MainViewController 添加到故事板并将其嵌入到导航控制器中.我建议将它嵌入到导航控制器中,以防您有其他想要显示的视图并且有一个返回到 MainViewController 的按钮.将 UIButton 添加到 LogInViewController 并控制从它现在拖动到导航控制器.选择当前模态转场.在故事板的大纲中选择 segue,然后在属性检查器中,给它起一个像present MainViewController"这样的名字.在 LogInViewController 的 ViewDidAppear 方法中,检查用户是否登录,如果是,则调用 [self performSegueWithIdentifier:@"present MainViewController" sender:nil]; 如果用户是已登录但未登录.MainViewController 中不会有后退按钮,因为它是以模态呈现的导航控制器的根视图控制器.如果你想让左上角的按钮成为用户退出的按钮,你可以在 MainViewController 的左侧添加一个 UIBarButton.现在转到 LogInViewController 代码并添加 - (IBAction) nameOfUnwindSegue : (UIStoryboardSegue *) segue{} 返回 Storyboard 并控制从此栏按钮拖动到视图控制器顶部的退出图标在故事板中.选择 nameOfUnwindSegue(您可以随意命名).这将创建一个 unwind segue,它将关闭视图控制器.您可以通过在情节提要中单击它来给定一个标识符,转到属性检查器,并给它一个标识符在 MainViewController 的 prepareForSegue 方法中,您可以检查 segue.identifier 是否等于标识符,如果是,则调用注销方法.

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.

这篇关于如何通过 Storyboard 向导航栏添加后退按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆