开发者

Objective-C - NSTimer still active when switched to another view

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:@"SecondViewContr开发者_StackOverflow中文版oller" bundle:nil];
[self presentModalViewController:newView animated:NO];
[newView release];

Thanks for any help!


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜