Search code examples
iosswiftsprite-kitxcode-storyboard

Xcode with swift: Is it possible to show a view controller upon another without resetting the first?


I'm creating a simple game with a pause button. I want to show another view controller with the pause screen without resetting the game view controller when i return. Every thing i have tried, has resulted in the game view controller being reset, when i press the resume button and return to the game. Does anybody know how i can do this without resetting the game? It's a sprite kit and swift iOS app in xcode.


Solution

  • You can show a pop-up based view. You can create .xib and design your pause view as you wish. Then you can resume the game without resetting anything.

    Sample code:

      // Set Pause Page
      var pauseView  = PauseView.loadFromNib() // need an extension
    
      @IBAction func Pause(sender: UIButton) {
        pauseView.btnResume.addTarget(self, action: #selector(resume(_:)), forControlEvents: .TouchUpInside)
        // set place on view
        view.addSubview(pauseView)
      }
    
      func resume(sender:UIButton) {
        pauseView.removeFromSuperview()
      }
    

    I created a demo for you:

    Full demo available on Github: Get Source Code