views:

116

answers:

3

i want to produce the delay of 2 sec using nstimer how to initialise timer in program?....

A: 

Here you go...

[NSTimer scheduledTimerWithTimeInterval:2 
                                     target:self 
                                   selector:@selector(action) 
                                   userInfo:nil 
                                    repeats:NO];
Lee Armstrong
+1  A: 

Multiple options here.

If you just want a delay of 2 seconds you could use the sleep() function

#include<unistd.h>
...
sleep(2);

Or you may be able to use NSTimer like so

[NSTimer    scheduledTimerWithTimeInterval:2.0    target:self    selector:@selector(fireMethod)    userInfo:nil repeats:NO];

And in your class you would have a method defined as

-(void)fireMethod
{
//Do stuff 
}
davydotcom
A: 

Note that you should not really be thinking about delays in an event driven UI/OS. You should thinking about tasks you want to do now, and tasks you want to do later, and code these subtasks and schedule them appropriately. e.g. instead of:

// code that will block the UI when done in the main thread
- (void) methodC {
  doA();
  delay(2);
  doB();
}

you might want to have code that looks more like:

- (void) methodA {
  doA();
  return;  // back to the run loop where other useful stuff might happen
}

- (void) methodB {
  doB();
}

and you can then schedule methodB with an NSTimer at the end of methodA, an NSTimer started by whatever called methodA, or, the best option, by the asynchronous completion routine of something started by methodA.

hotpaw2