开发者

Invalidate and release an NSTimer when popping a view (with a UINavigationController)

开发者 https://www.devze.com 2023-03-20 05:56 出处:网络
I\'m using an UINavigationController: in my first view there is an UIButton which pushes another view using this code

I'm using an UINavigationController: in my first view there is an UIButton which pushes another view using this code

- (IBAction)gototrack:(id)sender {
    map *map2 = [[map alloc] initWithNibName:@"map" bundle:[NS开发者_运维技巧Bundle mainBundle]];
    [self.navigationController pushViewController:map2 animated:YES];
    [map2 release];
}

In this second view there is a map which indicates the position of an object. This object is moving fast, so I need to refresh the position every second: I'm doing this with an NSTimer, which calls the method which calculates the new coordinates and refreshes the map. Ok, all works fine, but when the user pops the view (with the usual button in the NavigationBar) this second view could not be released, because the NSTimer is still working!

What can I do to invalidate and release the NSTimer when the user pops this second view? Are there some methods called automatically? (I tried but nothing)

Thanks!


You can try invalidating the NSTimer in -viewWillDisappear or viewDidDisappear.

viewWillDisappear:

This method is called in response to a view being removed from its window or covered by another view. This method is called before the view is actually removed or covered and before any animations are configured.

Subclasses can override this method and use it to commit editing changes, resign the first responder status of the view, or perform other relevant tasks. For example, you might use this method to revert changes to the orientation or style of the status bar that were made in the viewDidDisappear: method when the view was first presented. If you override this method, you must call super at some point in your implementation.

Have a look at the UIViewController reference for more details.


Easy, it is like this: [//Your nstimer// invalidate];

0

精彩评论

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