iOS Timer

iOS Timer: NSTimer

Start a timer to send a message

  • Execute a method with a scheduled timer (one time or repeatedly)
  • Set up a repeatable task when a view appear
    - (void)viewDidAppear:(BOOL)animated
    {
       [super viewDidAppear:animated];
       if (!myTimer) {
           myTimer = [NSTimer scheduledTimerWithTimeInterval:0.4
                                  target:self selector:@selector(doTimer:)
                                  userInfo:nil
                                  repeats:YES];
       }
       ...
    }
    
    - (void)doTimer:(NSTimer *)timer
    {
       ...
    }
    

Disable a timer when a view disappear

- (void)viewDidDisappear:(BOOL)animated
{
   [super viewDidDisappear:animated];
   [myTimer invalidate];
   myTimer = nil;
   ...
}

Schedule a task to send an iOS message

Send a message with a timer delay

[self performSelector:@selector(aMethod:)
           withObject:(id)argumentToAMethod
           afterDelay:(NSTimeInterval)wait];

Cancel a Scheduled iOS Message

Cancel a scheduled message

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(myMethod:) object:param];