Tuesday, May 10, 2011

An NSTimer example

An NSTimer example in XCode.  The NSTimer object is used to run effect method which updates the content of the label with current count. [NSTimer example]



NSTimer *timergogo;
int timercount=0;

- (void)effect:(NSTimer *)aTimer{
NSString *timerset = [[NSString alloc] initWithFormat:@"%d",timercount++];
timerz.text = timerset;
}
-(IBAction)timerstart:(id)sender{
timergogo = [[NSTimer scheduledTimerWithTimeInterval:0.1 target:self 
selector:@selector(effect:)
userInfo:nil
  repeats:YES]retain];
}
-(IBAction)timerstop:(id)sender{
//[timergogo release]; 
//timergogo = nil;
[timergogo invalidate];
}

Output screen.