Kasper Hansen
Kasper Hansen

Reputation: 72

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.

Upvotes: 1

Views: 463

Answers (2)

fatihyildizhan
fatihyildizhan

Reputation: 8832

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

Upvotes: 2

Pranav Wadhwa
Pranav Wadhwa

Reputation: 7746

Create a variable in your original view controller:

var shouldReset = true

Then, when you perform a segue back, add this in your prepareForSegue:

let vc = destination as! //Your view controller
vc.shouldReset = false

In your viewDidLoad (or wherever you reset your data):

if shouldReset == true {
    //Reset
}

Upvotes: 0

Related Questions