故事板中的 Modal 和 Push segue 有什么区别?
有人可以向我解释一下 modal
和 push
segue 之间的确切区别吗?
我知道当我们使用 push
时,segue 会添加到堆栈中,所以当我们继续使用 push
时,它会继续占用内存吗?
有人可以告诉我这两个是如何实现的吗?
Modal
segues 可以通过简单地ctrl-click 并拖动到目的地来创建,但是当我使用push
执行此操作时,我的应用程序崩溃了。
我从一个按钮推送到一个具有 UIViewController
的 UINavigationController
。
Can someone explain to me what is the exact difference between modal
and 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?
Can someone please show me how these two are implemented?
Modal
segues can be created by simply ctrl-click and dragging to destination but when I do that with the push
my app crashes.
I am pushing from a button to a UINavigationController
that has a UIViewController
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
push Segue 正在向导航堆栈添加另一个 VC。这假设发起推送的 VC 与要添加到堆栈的 VC 属于同一导航控制器的一部分。对于导航控制器和深堆栈来说,内存管理不是问题。只要您处理可能从一个 VC 传递到另一个 VC 的对象,运行时就会处理导航堆栈。请参阅图像以获取视觉指示:

模态 Segue 只是一个 VC 以模态方式呈现另一个 VC。 VC 不必是导航控制器的一部分,并且以模态方式呈现的 VC 通常被认为是呈现(父)VC 的“子”。模态呈现的 VC 通常没有任何导航栏或选项卡栏。呈现的 VC 还负责驳回它创建和呈现的模态 VC。
A push Segue is 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:

A modal Segue is 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.
Swift 3.0 和 XCode 8.2.1 更新
1. Push Segue
Push Segue 已重命名为显示 segue。要创建推送序列,父视图控制器需要嵌入导航控制器中。导航控制器提供导航栏。一旦您使用推送连接连接两个视图控制器,子视图控制器将自动在顶部显示导航栏。子视图控制器将添加到导航堆栈的顶部。
Push segue 还提供了默认功能。子视图控制器将有一个后退按钮,可让您返回到父视图控制器。您还可以向右滑动以弹出子视图控制器。 Push Segue 的动画就像水平滑动页面一样。
虽然您可以从不在导航控制器中的视图控制器进行推送,但这样做时您将失去所有功能,例如导航栏、动画、手势等。在这种情况下,您应该首先将父视图控制器嵌入到导航视图控制器中,然后将推送转场到子视图控制器。
2. 。
另一方面,模态 Segue(即模态呈现)呈现在当前视图控制器上 子视图控制器不会继承导航视图控制器,因此如果您使用导航视图控制器从视图控制器呈现模态转场,导航栏将会丢失。如果您想要恢复子视图控制器,则必须再次将子视图控制器嵌入到导航控制器中并启动一个全新的导航堆栈。如果你想返回父视图控制器,你必须自己实现它并从代码中调用
dismiss
。模态转场的动画是子视图控制器将从页面底部出现。导航视图控制器在此演示中也消失了
Swift 3.0 and XCode 8.2.1 update
1. Push Segue
Push segue has been renamed as Show segue. 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 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.
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.
2. Modal Segue
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.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
推送视图必须构建在
navigationController
中。单击您的
主视图
,然后在菜单栏中选择:The push view must be built in a
navigationController
.Click on your
master view
, then in the menu bar choose:这是使用 Storyboard 的自定义推送和 Segue 方法来推送控件
而 Modal 是在不使用 Storyboard 的情况下浏览视图的方法。
This is pushing controls using custom push and segue methods for storyboard
And Modal is way to navigate through views without using Storyboards.