2

I got a timer running on my first view controller, then i want to switch to the other view controller and the timer is still running in the background.

How can i whole clean up the first view controller and launch the second view controller.

SecondViewController *newView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self presentModalViewController:newView animated:NO];
[newView release];

Thanks for any help!

Flappy
  • 848
  • 15
  • 31

2 Answers2

1

Well it depends on what you're doing with the views. If you're using UITabBarController or UINavigationController you'll probably not want to "clean up" the first view controller as you launch the second one. You want the controllers to handle that. So if you are using either of these controllers, I'd suggest pausing the timer when the first view goes out of view using the UIViewController's viewWillDisappear: method.

john
  • 3,043
  • 5
  • 27
  • 48
0

I suspect the problem is your first view is still alive (retained) in memory because you have just presented the second view as a modal view. The second view is able to be dismissed because the parent is still retained.

What you need to do is maybe move the timer to your app delegate or a singleton class. Then give yourself a couple methods to start / stop / pause it at will.

Or, you could simply pause or stop the timer on the viewDidLoad call of the second view, then resume or restart it on the viewDidUnload.

You can get at the parent view by using the parentViewController property of the second view controller.

Matthew Carriere
  • 511
  • 6
  • 13
  • Oke, thanks for you answer. But there is no way to clean up the UIViewController because i'm not using it? The timers are created inside the first UIViewController class. But there also timer that created automatically when using the app (game) – Flappy Aug 15 '11 at 23:07
  • No, you can't get rid of the first view controller because its the parent in a presentModal relationship. If you want to blow away your first controller then you should maybe be using UITabBarController, and then making the first and second view controllers tabs. – Matthew Carriere Aug 15 '11 at 23:13
  • The ViewControllers in an iPhone application are far more chained together than you may be used to as in web programming. :) – Matthew Carriere Aug 15 '11 at 23:14
  • Oke, thanks. When i use my code above to switch to the second view controller and then switch back to the first with the same code. The first view controller is then not twice active? – Flappy Aug 15 '11 at 23:17
  • You wouldn't be presenting modal view anymore. Here's a link to the question [Is there a good UITabBarController example?](http://stackoverflow.com/questions/3011262/is-there-a-good-uitabbarcontroller-example) – Matthew Carriere Aug 15 '11 at 23:23