0

I'am new to ios. I have several view controllers and I want back button to get user to level up controller. Example.

But if user comes from gameOver view, back button sent him back to gameOver and I don't want such behavior (I want user to be sent at second level (games level) controller as shown). On android I could set the pop behavior for the navigation actions with mouse very easily.

What is the correct way to do the same in ios? Or I have to create the custom back button and do everything manually?

Dima
  • 893
  • 1
  • 4
  • 11
  • If you are using storyboards (which I would recommend) you can use an unwind segue: https://stackoverflow.com/questions/12561735/what-are-unwind-segues-for-and-how-do-you-use-them – Simon McLoughlin Dec 04 '19 at 12:13
  • Thank you for the answer. Is it possible to use unwind segue with back button? Or I have to do custom button for that – Dima Dec 04 '19 at 12:35
  • the second answer to this question says you can do it as the default back action, and he has provided a sample github project with it working. I've never tried it though. If that doesn't work, then yes you'll need a custom button: https://stackoverflow.com/questions/29236656/unwind-segue-function-with-back-button – Simon McLoughlin Dec 04 '19 at 14:03

1 Answers1

0

Using Swift this can be achieved using below code:

func popToViewController(_ specificViewController: Swift.AnyClass) {
 let viewControllers = self.navigationController!.viewControllers
 for eachViewController in viewControllers {
     if eachViewController.isKind(of: specificViewController) {
         self.navigationController!.popToViewController(eachViewController, animated: true)
         break;
     }
 }
}

Usage:

self.popToViewController(gameLevelViewController.self)