1

I am programming a game. It is a word game. Game page is always the same with different content. when i use instantiateViewControllerWithIdentifier("SB"), new page is added to the queue.but That way memory used goes high.

how can i replace old page with new one ?

Recep Bala
  • 19
  • 1
  • 5

1 Answers1

0

You can keep a reference to the class and have a method in it that loads the content inside it.

class gameViewController : UIViewController {

   func loadContent() {
      //  this method should refresh content everytime.
    }

}

In some other class, where ever you wish to, just create a reference to previous class.

class SomeClass { 
  var referenceToGameClass : gameViewController!


   func someFunc() {
      self.referenceToGameClass.loadContent()
   }
}

But how can you save the same reference while going out of the gameViewController class? You can add this line in your prepareforsegue method maybe, let's say while going to above mentioned 'SomeClass' :

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
      if(segue.identifier == "segueID") {
          let svc = segue.destinationViewController as! SomeClass
        svc.referenceToGameClass = self
    }

}

And conveniently, to move out of 'SomeClass' back to same gameViewController, simply use :

self.presentViewController(self.referenceToGameClass, animated: true, completion: nil)

This can get a bit confusing, but if you read it a couple times carefully, you will get it. You can also ask me questions!

Akshansh Thakur
  • 5,163
  • 2
  • 21
  • 38
  • Thank you for your answer. I have already done these.. My problem is with viewControllers ( game playing page). When user win and pass to new page, i use "presentViewController" for a new page (actually the same view and Controller with new content ). Problem part is New Pages are added to storyboard stack if i use presentViewController. this way used memory increases. I dont want memory goes so high unnecessarly. – Recep Bala Jun 19 '16 at 14:30
  • Have you thought about using navigation controller? This will create a horizontal flow from first view controller to the last view controller. The benefit would be that you can use navigation controller methods – Akshansh Thakur Jun 19 '16 at 14:38
  • Check this to understand : http://stackoverflow.com/questions/3852932/calling-poptorootviewcontrolleranimated-after-dismissmodalviewcontrolleranimated – Akshansh Thakur Jun 19 '16 at 14:38