ios Storyboards 中的 Modal 和 Push segue 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9392744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between Modal and Push segue in Storyboards?
提问by Gaurav_soni
Can someone explain to me what is the exact difference between modal
and push
segue?
有人可以向我解释modal
和push
segue之间的确切区别是什么?
I know that when we use push
the segue gets added to a stack, so when we keep using push
it keeps occupying memory?
我知道当我们使用push
segue 时会被添加到堆栈中,所以当我们继续使用push
它时会一直占用内存?
Can someone please show me how these two are implemented?
有人可以告诉我这两个是如何实现的吗?
Modal
segues can be created by simply ctrl-clickand dragging to destination but when I do that with the push
my app crashes.
Modal
可以通过简单地ctrl-click拖动到目的地来创建 segues ,但是当我这样做时,push
我的应用程序崩溃了。
I am pushing from a button to a UINavigationController
that has a UIViewController
.
我正在从一个按钮推到一个UINavigationController
带有UIViewController
.
采纳答案by LJ Wilson
A push Segueis adding another VC to the navigation stack. This assumes that VC that originates the push is part of the same navigation controller that the VC that is being added to the stack belongs to. Memory management is not an issue with navigation controllers and a deep stack. As long as you are taking care of objects you might be passing from one VC to another, the runtime will take care of the navigation stack. See the image for a visual indication:
甲推Segue公司是加入另一VC到导航堆栈。这假设发起推送的 VC 是添加到堆栈的 VC 所属的同一导航控制器的一部分。内存管理是不具有导航控制器和深栈的问题。只要您处理可能从一个 VC 传递到另一个 VC 的对象,运行时就会处理导航堆栈。请参阅图像以获取视觉指示:
A modal Segueis just one VC presenting another VC modally. The VCs don't have to be part of a navigation controller and the VC being presented modally is generally considered to be a "child" of the presenting (parent) VC. The modally presented VC is usually sans any navigation bars or tab bars. The presenting VC is also responsible for dismissing the modal VC it created and presented.
一个模式Segue公司仅仅是一个VC提出另一个VC模态。VC 不必是导航控制器的一部分,并且模态呈现的 VC 通常被认为是呈现(父)VC 的“孩子”。模态呈现的 VC 通常没有任何导航栏或标签栏。呈现的 VC 还负责解除它创建和呈现的模态 VC。
Hope this helps.
希望这可以帮助。
回答by Fangming
Swift 3.0 and XCode 8.2.1 update
Swift 3.0 和 XCode 8.2.1 更新
1. Push Segue
1. 推赛格
Push segue has been renamed as Showsegue. To create push segue, the parent view controller needs to be embedded in navigation controller. The navigation controller provides navigation bar. Once you connect two view controller with push segue, the child view controller will automatically has navigation bar on top. The child view controller will be added on top of the navigation stack.
Push segue 已重命名为Showsegue。要创建推送转场,父视图控制器需要嵌入到导航控制器中。导航控制器提供导航栏。使用 push segue 连接两个视图控制器后,子视图控制器将自动在顶部显示导航栏。子视图控制器将添加到导航堆栈的顶部。
Push segue also provides default features. The child view controller will have a back button that gets you back to the parent view controller. You can also swipe right to pop the child view controller. The animation for push segue is like sliding pages horizontally.
Push segue 还提供默认功能。子视图控制器将有一个返回按钮,可让您返回父视图控制器。您也可以向右滑动以弹出子视图控制器。push segue 的动画就像水平滑动页面。
While you are allowed to make a push segue from a view controller that is not in a navigation controller, you will lose all the features like navigation bar, animation, gesture etc when you do so. In this case, you should embed your parent view controller inside navigation view controller first and then make push segue to child view controllers.
虽然您可以从不在导航控制器中的视图控制器进行推送转场,但这样做时您将失去导航栏、动画、手势等所有功能。在这种情况下,您应该先将父视图控制器嵌入到导航视图控制器中,然后再将推segue 推送到子视图控制器。
2. Modal Segue
2. 模态转场
A modal segue (i.e. present modally), on the other hand, is presenting over the current view controller. The child view controller will not inherit navigation view controller so the navigation bar will be lost if you present modal segue from a view controller with navigation view controller. You have to embed the child view controller in navigation controller again and start a brand new navigation stack if you want it back. If you want to get back to parent view controller, you have to implement this by yourself and call dismiss
from code.
另一方面,模态转场(即模态呈现)正在当前视图控制器上呈现。子视图控制器不会继承导航视图控制器,因此如果您从带有导航视图控制器的视图控制器呈现模态转场,导航栏将丢失。你必须再次将子视图控制器嵌入到导航控制器中,如果你想要它回来,还需要启动一个全新的导航堆栈。如果你想回到父视图控制器,你必须自己实现并dismiss
从代码调用。
Animation for modal segue is that the child view controller will comes up from the bottom of the page. The navigation view controller is also gone in this demo
模态转场的动画是子视图控制器将从页面底部出现。导航视图控制器在这个演示中也消失了
回答by Simone Pistecchia
The push view must be built in a navigationController
.
推送视图必须构建在navigationController
.
Click on your master view
, then in the menu bar choose:
单击您的master view
,然后在菜单栏中选择:
EDITOR->embed in->navigationController
回答by Pushkraj
This is pushing controls using custom push and segue methods for storyboard
这是使用故事板的自定义推送和 segue 方法推送控件
And Modal is way to navigate through views without using Storyboards.
Modal 是一种在不使用 Storyboard 的情况下浏览视图的方法。