Reputation: 196
I just happened to realize that NSTimer
is still firing when i'm navigating away from a ViewController
like pressing back button. What is the appropriate way to terminate that particular NSTimer
?
Upvotes: 0
Views: 2269
Reputation: 1680
This is what I understood from your question:
That You want to invalidate timer while going to another ViewController right?
So try this:
YourViewController *objController = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil];
[self.navigationController pushViewController:objController animated:YES];
[timer invalidate]; //your timer object goes here
EDIT:
or if you are Popping Up the ViewConrtoller/ Dismissing viewContoller, then put
[timer invalidate];
afer that block of code.
Upvotes: 0
Reputation: 13354
You can use this method for terminate your NSTimer
-
[timer invalidate]; //timer is your NSTimer
You can invalidate
your timer
like this -
1. Method
-(void)viewWillDisappear:(BOOL)animated
{
[timer invalidate];
}
2. Method
-(void)viewDidDisappear:(BOOL)animated
{
[timer invalidate];
}
Upvotes: 0
Reputation: 1460
I would suggest you to make custom navigationcontroller class using which you can have an event while poping viewcontroller.
#import "customNavigationController.h"
#import "SettingsTableController.h"
@implementation customNavigationController
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
if([[self.viewControllers lastObject] class] == [SettingsTableController class]){
[[(SettingsTableController *)[self.viewControllers lastObject] timer] invalidate];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 1.00];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown
forView:self.view cache:NO];
UIViewController *viewController = [super popViewControllerAnimated:NO];
[UIView commitAnimations];
return viewController;
} else {
return [super popViewControllerAnimated:animated];
}
}
@end
here, SettingsTableController is the class which is having NSTimer , so u can invalidate your timer. This will help not to invalidate timer while presenting any modalview or even pushing a view controller.
more details http://www.hanspinckaers.com/custom-action-on-back-button-uinavigationcontroller
Upvotes: 0
Reputation: 1120
You can handle it in the viewDidDisappear event:
-(void)viewDidDisappear:(BOOL)animated {
[myTimer invalidate];
}
Upvotes: 4