-2

I have 2 viewControllers. One of them is for the game screen and it has lots of different variables like labels,buttons,images. The other one is for the pause menu. When i pause the game and tap the resume button, it calls the viewDidLoad of the first viewController naturally.

I want to save the last state of the game when user tap the pause button. Or i want to see the last state of the game when user tap the resume button. I don't want to use nsuserdefault for this,if there is a different solution. Hope i can explain my problem.

Best Regards, Taha

Taha
  • 31
  • 8

1 Answers1

1

When i pause the game and tap the resume button, it calls the viewDidLoad of the first viewController naturally

Then you're doing this wrong. There is no reason why the first view controller (with the game screen) should be torn down merely because you are showing the pause menu view controller. You need to rethink your architecture here.

For example, if the first view controller presents the second view controller, then the second view controller's view appears, but the first view controller is not torn down. When the second view controller vanishes again (resume), the first view controller's viewWillAppear: and viewDidAppear: will be called, but not viewDidLoad. Your job is to organize your tasks so that the game pauses on viewWillDisappear: and resumes on viewDidAppear: but the data displayed is not torn down or reset.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Yes, i'm doing wrong but i try to learn how i can show the last state of the 1st viewController when i tap the resume button which belongs the 2nd viewController. – Taha Jan 03 '15 at 16:46
  • 1
    @Taha : for that thing you don't need to load your first View Controller , you can store your last state & inside app delegate finish launching methods you can load your View Controller according to state of game. – Krunal Darji Jan 03 '15 at 16:49
  • You don't need to "show the last state" because the view never went away. So when you tap resume, you _dismiss_ the second view controller which you _presented_ earlier; the second view controller's view vanishes, there is the first view, the game and all its state, just as you left it previously. That's the point. – matt Jan 03 '15 at 16:55
  • Here's what a presented view controller is: http://www.apeth.com/iOSBook/ch19.html#_presented_view_controller – matt Jan 03 '15 at 16:56
  • Now i realised that my approach was totally wrong. Thank you so much for helping this newbie. – Taha Jan 03 '15 at 17:04