简体   繁体   中英

How do I segue to another viewController that shares the same root controller as the current one?

I have 3 viewControllers : A, B and C.
A has a segue to B and A also has a segue to C.

If I'm currently in C, how do I segue over to B?

Note; I need to keep A active because once I'm in B, I need to be able to unwind back to A.

The following solution would be nice if I could unwind from C to A and then push to B without showing A in the process, but I can't get this to work, and no-one has provided a solution.

Chain two segues to look like one

Note; I'm not using a UINavigationController . I've tried simply pushing from C to B but then I'm not able to deallocate C. Calling unwind on C at any point during this process gives weird behaviour where A might appear on screen at some point.

Does anyone have a solution to this?

There is plenty of way to accomplish what you want.

I would do something really easy: that is to add an unwind segue (it's explained above how to add one) to exit to the first A vc, and add a custom segue (no matter which one) between B and C (that you simply create with the storyboard).

You start in A, then:

  • If you go to B, you can come back using the self.dismiss , your using your exit segue.
  • If you go to C, you can come back to A the same way, or you can go to B.
  • Once you are in B (from: A to C, C to B), you can come back to C using self.dismiss or you can come back to A using the exit segue.

To add un unwind segue, you must add a function to your first VC of the type:

@IBAction func unwindToA(segue: UIStoryboardSegue)

Then, in the storyboard when you are creating your segue on the your B view controller (using control+drag), start to drag from the VC icon (on the top of the screen) to the exit icon. Then, you will see appear a new segue, named 'unwindToA', which will take you back to your first VC: A.

您可以在此处(屏幕顶部)看到这些图标

To create your segue from B to C, you can do by control+drag from the first to the second VC in your storyboard, and choose the type you prefer (or you can create a custom segue if those provided don't correspond to want you expect).

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