我刚刚意识到NSTimer
在我离开ViewController
时按下后退按钮时仍在发射。终止该特定NSTimer
的适当方法是什么?
答案 0 :(得分:4)
您可以在viewDidDisappear事件中处理它:
-(void)viewDidDisappear:(BOOL)animated {
[myTimer invalidate];
}
答案 1 :(得分:0)
您可以使用此方法终止NSTimer
-
[timer invalidate]; //timer is your NSTimer
你可以invalidate
timer
这样 -
<强> 1。方法强>
-(void)viewWillDisappear:(BOOL)animated
{
[timer invalidate];
}
<强> 2。方法强>
-(void)viewDidDisappear:(BOOL)animated
{
[timer invalidate];
}
答案 2 :(得分:0)
这是我从你的问题中理解的:
您希望在转到另一个ViewController时使计时器无效吗?
所以试试这个:
YourViewController *objController = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil];
[self.navigationController pushViewController:objController animated:YES];
[timer invalidate]; //your timer object goes here
修改强>
或者如果你正在弹出ViewConrtoller / Dismissing viewContoller,那么就把
[timer invalidate];
那段代码。
答案 3 :(得分:0)
我建议你制作自定义的navigationcontroller类,使用它来弹出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
这里,SettingsTableController是具有NSTimer的类,所以你可以使你的计时器无效。这将有助于在呈现任何模态视图或甚至推动视图控制器时使计时器无效。
更多详情http://www.hanspinckaers.com/custom-action-on-back-button-uinavigationcontroller