0

I am trying to unwind to a specific viewController without using navigationController. For instance, I have a set of questions the user needs to answer. Depending on their answer they either need to get unwound to the root controller or the first question. I am aware of how I would go about navigating them to the root controller, I just don't know how to go back to any other specific controller. I am under the understanding that if I were to use a segue to go from viewController 4 -> 2 it would use more memory and the stack could end up like 1->2->3->4->2 etc...

Any suggestions would be greatly appreciated.

bcupp
  • 61
  • 1
  • 10
  • you can just make use of navigationController function call as self.navigationController?.popToViewController(VC, animated: true) Just specify where you wanted to go – iOS Geek Nov 20 '17 at 04:45
  • For some reason when I try that it says I cannot convert TestViewController.type to expected argument UIViewController. Any ideas why it might say that? – bcupp Nov 20 '17 at 05:12
  • I think you are passing .type value instead of UIVewcontroller use VC declaration as let VC = self.storyboard?.instantiateViewController(withIdentifier: "ChekViewController") as! ChekViewController – iOS Geek Nov 20 '17 at 05:17
  • Can you show your code? – Adeel Miraj Nov 20 '17 at 05:17
  • If i am understanding your question,u r not using navigation controller? – Sumit Jangra Nov 20 '17 at 05:26
  • Check this: https://stackoverflow.com/questions/45792130/unwind-then-segue-without-showing-intermediate-view-controller/45792543#45792543 – Amit Nov 20 '17 at 06:09
  • Thank you all for your help! I was able to get it working with Andras M's solution. – bcupp Nov 20 '17 at 14:52

2 Answers2

0

In each VC you want to unwind to you need to create an @IBAction func with one argument (_ segue UIStoryboardSegue) then in Your storyboard you go to the vc you want to unwind from and on the top control click drag between th VC icon and the exitnicon. You need to choose the unwind function and then a new segue appears in your VC tree on the left hand side. Clicking on the segue gives you an option as with regular soryboard segues togive it an ID. Once named you can programatically perform segue with identifier. You can add as many of these segues as you like.

Andras M.
  • 838
  • 7
  • 12
-1

You can use something like the following method to pop as many views at a time as you need.

+ (void)popBackWithSourceViewController:(UIViewController *)viewController viewsToPop:(int) viewsToPop {            
    [viewController.navigationController popToViewController:viewController.navigationController.viewControllers[viewController.navigationController.viewControllers.count-viewsToPop-1];
}
nanibir
  • 234
  • 1
  • 9