views:

20

answers:

2

Hi all,

I am implementing an iPhone application. In that I need to use a timer to call a method to get the values frequently. And Also I need to stop the Timer in another method. So I need to use it as global. But I don't an idea to create a global Timer can you guys please help on this.

Thanks in Advance, Sekhar Bethalam.

A: 

In the interface, declare a property:

@interface myViewController : UIViewController 
{
    NSTimer *timer;
}
@property (nonatomic, retain) NSTimer    *timer;

@end

and then synthesise it in the .m file:

@implementation myViewController

@synthesize timer;

and then whenever you need to use it, just use timer.

Felixs
Thanks for your answer.
sekhar
A: 

Just keep a reference to the timer.

In .h file:

NSTimer *timer;

..

@property (nonatomic, retain) NSTimer *timer;

In .m file:

@synthesize timer;

..

// Initiation:
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];

..

// Termination:
[self.timer invalidate];
Michael Kessler
Thanks for you answer
sekhar