Method invocation with timer

2 Comments 17th APR 2009 | Posted by Ivan Kalaica in iPhone dev

Method invocation with timer

Method invocation with timer is something every developer sometime needs. First you make an instance of NSInvocation class and you add invocationWithMethodSignature.

invocationWithMethodSignature consists of method selector (which method your invocation will trigger) and target (which object is target). Next you make instance of NSTimer class to define repeat interval. See example below.

- (void) viewDidLoad {

NSInvocation *updateDisplayInvocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector: @selector (myMethod)]];

[updateDisplayInvocation setSelector: @selector (myMethod)];

[updateDisplayInvocation setTarget: self];

NSTimer *audioDisplayUpdateTimer;

audioDisplayUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 invocation:updateDisplayInvocation repeats:YES];

[super viewDidLoad];

}

- (void) myMethod {

NSLog(@"Method execution");

}

And that is it!

If you like what you read, please share it.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Yahoo! Bookmarks
  • Reddit
  • Technorati
  • Twitter
  • StumbleUpon
  • LinkedIn
  • Netvibes
  • NewsVine
  • Sphinn
  • Tumblr
  • Posterous

To post code in comments, place your code inside [code] and [/code] tags.

There are 2 comments (Add Yours +)

  • PRASENJIT SINHA Says

    Hi,
    I have tried the same thing what you have explained above but after the time interval it is not at all going inside the method “my method”.
    Please help me out.
    Thanks

  • Ivan Kalaica Says

    Hm, create new xcode project and paste this code to viewController that is initially loaded. It works just fine.

Leave a Comment

Please wrap all source codes with [code][/code] tags.
Magento Design and Development | Magento SEO | iPhone Application Development Web Application Development with ZEND | WordPress Ecommerce | WordPress development
Sitemap

Inchoo - webappsolutions | 2009