I am using
[NSTimer scheduledTimerWithTimeInterval:0.1f
target:self
selector:@selector(update)
userInfo:nil
repeats:YES];
I want to stop calling this timer one.
viewDidDisappear
How can i do that? invalidate?
Declare NSTimer *myTimer
in .h file.
Assign instance as tom said like this
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.1f
target:self
selector:@selector(update)
userInfo:nil
repeats:YES];
Stop and Invalidate using this
- (void) viewDidDisappear:(BOOL)animated
{
[myTimer invalidate];
myTimer = nil;
}
Try this
viewController .h
NSTimer *timer;
viewcontroller.m
timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(pollTime)
userInfo:nil
repeats:YES];
- (void) viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[timer invalidate];
}
Yes, you would use the - (void)invalidate
instance method of NSTimer
.
Of course, to do that, you would have to save the NSTimer
instance returned from [NSTimer scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:]
into an ivar or property of your view controller, so you can access it in viewDidDisappear
.
invalidate
Method of NSTimer
is use for stop timer
- (void) viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[self.timer invalidate];
self.timer = nil;
}
If you are not ARC then don't forget [self.timer release];
For stopping or invalidating NSTimer first you have to create instance for NSTimer in Globally.
Timer=[NSTimer scheduledTimerWithTimeInterval:0.1f
target:self
selector:@selector(update)
userInfo:nil
repeats:YES];
after that try like this,
if (Timer != nil) {
[Timer invalidate];
Timer = nil;
}