unwind segue vs. popViewControllerAnimated

2019-03-03 10:27发布

问题:

I have a mapview that is connected to several viewControllers.

I needed to implement an 'unwind' from another button (as opposed to just the back button), so I used:

self.navigationController?.popViewControllerAnimated(true)

My question is: as the above works, should I even bother trying to implement unwind in code using prepareForUnwind and canPerformUnwindSegueAction in the parent view controller + ctrl-drag from the viewController to exit in Storyboard? If so, why?

回答1:

Basically if you are just dimissing a presented UIViewController or popping a UIViewController from the navigation stack,you don't need to use unwind segues although it does the same for you.

But think of a case where there are UINavigationControllers A,B,C and then a RootViewController R

Then think of a condition

 (Present) R -> A
 (Push) AR -> XVController
 (Push)XVController -> B
 (Present)BR-> YVContoller
...and so on..

Now if you want to go back to YVController to your project RootViewController where you just started up.There is no way as popToRootViewController pops to the navigation stack of the B UINavigationController.Now what you do..Either use delegate or change window RootViewController which is not a good idea.

So you need to use unwind segue to overcome this.. A sample project to demonstrate the power of using it..