简体   繁体   English

在导航堆栈中模态显示视图控制器

[英]Presenting a view controller modally within a navigation stack

here's my situation: 这是我的情况:

I have two view controllers embedded in a Navigation controller. 我在导航控制器中嵌入了两个视图控制器。 I'll call them Sender and Destination 我称它们为“发件人”和“目的地”

Basically what I'm trying to achieve is to "replace" the Sender with the Destination in the navigation stack such that pressing a button on the Sender segues to the Destination but pressing the "< Back" button in the navigation bar for both the Sender and the Destination will lead to the same view controller. 基本上,我要实现的是在导航堆栈中将“发送方”与“目标”“替换”,使得按下“发送方”上的一个按钮会选择“目标”,但同时按下导航栏中的“ <返回”按钮和目标将导致相同的视图控制器。

Here's the problem: I want the transition between these two view controllers to be a Horizontal Flip, so I created a segue connecting the Sender and the Destination with kind "Present Modally", transition "Flip Horizontal", . 这是问题所在:我希望这两个视图控制器之间的过渡是“水平翻转”,所以我创建了一个连接“发送者”和“目标”的segue,类型为“呈现模态”,过渡为“水平翻转”,。 In Sender I select the "Defines Context" option. 在发件人中,我选择“定义上下文”选项。

Now, for the connecting segue: 现在,对于连接序列:

-When I select "Over Current Context" for presentation it does exactly what I want! -当我选择“ Over Current Context”进行演示时,它确实满足我的要求! ...Except the animation looks a little wonky. ...除了动画看起来有点古怪。 Basically the Sender stays flat and static while the Destination flips above it, as opposed to them flipping "together". 基本上,发件人保持平坦和静态,而目标翻转到其上方,而不是它们“一起翻转”。 This makes sense because the segue is presenting over the Sender, but it's not quite what I'm looking for. 这是有道理的,因为在发送方上显示序列,但这并不是我想要的。

-When I select "Current Context" for presentation the animation looks great! -当我选择“当前上下文”进行演示时,动画看起来很棒! ...Except if I press the "< Back" button in the navigation bar, it just leads to a plain black view with a navigation bar and if I press the "< Back" button again on this view I get the following exception: ...除非我按导航栏中的“ <后退”按钮,否则只会导致带有导航栏的纯黑色视图,如果再次在该视图上按“ <后退”按钮,则会得到以下异常:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't add self as subview'

Anybody have any idea how I can achieve what I'm trying to do? 有人知道我如何才能实现我的目标吗? Is this even the right approach? 这是正确的方法吗? I've seen some suggestions to try and flip the view controller's view to a new view instead of flipping the entire view controller to a different view controller. 我已经看到了一些建议,可以尝试将视图控制器的视图翻转到新视图,而不是将整个视图控制器翻转到另一个视图控制器。 But is it possible to have two "full-sized" views for one view controller in the storyboard? 但是,故事板中的一个视图控制器是否可以具有两个“全尺寸”视图?

Thanks! 谢谢!

I'm sure there are various approaches that could work, but my first thought would be to create a container view for the Sender and Destination views. 我确信可以使用多种方法,但是我首先想到的是为“发件人”和“目标”视图创建一个容器视图。

So you have ... 所以你有了 ...

  • NavigationController with Root View Controller ("RVC") 带有根视图控制器(“ RVC”)的NavigationController
  • Container View Controller ("CVC") 容器视图控制器(“ CVC”)
  • Sender VC 发件人VC
  • Destination VC 目标VC

Instead of "RVC" segueing to "Sender" you segue to "CVC" ... "CVC" initially loads "Sender" into its "container view" ... At this point, tapping <Back takes you, naturally, back to "RVC" 而不是将“ RVC”设置为“发件人”,而是将其设置为“ CVC” ...“ CVC”最初将“发件人”加载到其“容器视图”中。在这一点上,点击<Back自然会带您回到“RVC”

When you tap a button (or whatever you use to initiate the next transition) to go to "Destination" you can use a transition with "replace Sender with Destination" inside the container view ... and at that point, since "CVC" is still the top-of-the-stack view controller, tapping <Back will again take you back to "RVC" 当您点击按钮(或用于启动下一个过渡的任何方法)转到“目标”时,可以在容器视图内使用“将发送方替换为目标”的过渡...此时,因为“ CVC”仍然是堆栈顶部的视图控制器,请点击<Back再次将您带回到“ RVC”

Hope that makes sense :) 希望有道理:)

Demo example available here: https://github.com/DonMag/ContainerViewExample 演示示例可在此处获取: https : //github.com/DonMag/ContainerViewExample

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

相关问题 模态呈现视图控制器 - Presenting a View Controller Modally 导航控制器中的UIViewController并以模态呈现 - UIViewController in navigation controller AND presenting modally 在导航控制器中模样演示情节提要 - Modally presenting Storyboard in Navigation Controller 在具有分段控件的UITabBarController中模拟呈现导航视图控制器 - Modally Presenting a Navigation View Controller in a UITabBarController With a Segmented Control 呈现视图控制器以模态重置当前视图 - Presenting a View Controller Modally Resets The Current View 如何检查视图控制器是模态呈现还是推送到导航堆栈上? - How to check if a view controller is presented modally or pushed on a navigation stack? 在以模态方式呈现视图控制器时,如何将视图控制器从搜索结果推入导航堆栈? - How do I push a view controller onto the nav stack from search results when presenting them modally? 横向模式下的导航控制器堆栈,但模式呈现的视图控制器视图始终为纵向帧大小 - Navigation controller stack in landscape mode, but modally presented view controller view always in portrait frame size 模态显示所选表行中的新视图控制器 - Presenting a new view controller from selected table row modally 模态显示视图控制器时应用程序崩溃 - Application is getting crash while presenting view controller modally
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM