PDA

View Full Version : timer value




superbeginer
Sep 20, 2009, 02:04 PM
hi i beginer in obj c
how get value in timer, my code :

- (void)viewDidLoad {
timeval =[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(mypos) userInfo:nil repeats:YES];

[super viewDidLoad];
}

int timecount = 5;
-(void)mypos{
int posx;
posx = (arc4random() % 200)+10;

if(timecount ==5){
NSLog(@"My posx %d", posx);

}

if(timecount == 0){
NSLog(@"My posx %d", posx);
[timeval invalidate];
}
timecount = timecount -1;
}

debug :
(timecount == 5) My posx 183
(timecount == 0) My posx 96

how to get value to other class, thanks



firewood
Sep 20, 2009, 02:37 PM
Counting ticks doesn't always work properly.

Try:

[ NSDate timeIntervalSinceReferenceDate ]

or

mach_absolute_time()

or

gettimeofday()

and save a reference time from when you start your NSTimer.