简体   繁体   中英

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", . 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! ...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")
  • Container View Controller ("CVC")
  • Sender VC
  • Destination 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"

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"

Hope that makes sense :)

Demo example available here: https://github.com/DonMag/ContainerViewExample

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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