Search code examples
iphonexcodenstimernstimeinterval

I should able to delay 2 seconds using NSTimer. How to do it?


I want to produce a delay of 2 seconds using NSTimer how to initialize timer in program?


Solution

  • 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 
    }