If you want to scheduled a task, NSTimer can help you. Here comes the example.
MyViewController.h
... @interface MyViewController : UIViewController { NSTimer *aTimer; } @property (nonatomic, retain) NSTimer *aTimer; ...
MyViewController.m
... @synthesize aTimer; ... /* Schedule a task whenever the view is load */ - (void)viewDidLoad { [super viewDidLoad]; // Schedule the runScheduledTask in 5 seconds aTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(runScheduledTask) userInfo:nil repeats:NO]; } /* runScheduledTask */ - (void)runScheduledTask { // Do whatever u want ... // Set the timer to nil as it has been fired aTimer = nil; } /* Don't forget to release the objects */ - (void)dealloc { [aTimer release]; [super dealloc]; }
Done =)
i think you mean @synthesize instead of @synthesis 😀
LikeLike
haha~ you are right, i have corrected it. Thanks. =)
LikeLike
m trying to program a sample timer …the problem m facing is that it never jumps to the target method……nothing happens and the next line is executed…..can someone tell me what’s wrong with it…!!!
LikeLike
what is the purpose of the NSTimer input argument?
-(void)tryb:(NSTimer *)timer
And there is a colon for the selector argument (tryb:).
NSTimer *timer=[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(tryb:) userInfo:nil repeats:YES];
LikeLike
well in most of the programs I have seen it this way even in Mac os x API documentation I have seen it this way……
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html
I have referred this…..!!
LikeLike
oic, i just realized i didn’t follow the Apple API. =P
i have no idea why it does not work and seems you are not developing a iphone application. could u send me the example project?
LikeLike
well actually I am definitely developing for mac os and what it refers to is for Mac as well…though I solved the problem by just removing the semicolon in selector method (as u suggested)and its working great ..sorry can’t send project details….its confidential!!
LikeLike
No problem. Good to know that you have resolved it. =)
LikeLike
Hi thank you for this function but I have problem no action happen it’s mean not working with my code please could you check on my could is very simple
LikeLike
you are instantiating a Home object for every 0.1 second?
what about if u only print a log in the runScheduledTask, does it work?
LikeLike
Hi thanks for quick reply I try to switch to another view like 5 sec or 1 however but still not working they are no error any idea ?
LikeLike
first let’s figure out if the NSTimer works or not.
what happen if you replace the runScheduledTask as follow?
is the log printed?
LikeLike
I did but still no action and the log file is empty.
here the code for h file
—–
Thanks Man 🙂
LikeLike
O, you should put the code in ViewDidLoad instead of ViewDidUnload.
i didn’t notice that. =P
and my previous comment was wrong, you only run the timer once because of repeats:NO.
does it solve the problem?
LikeLike
No man , but which code you say ?
LikeLike
what u are doing now is setting up the aTimer in the ViewDidUnload.
This code will run only when the View is UNLOADED. That’s y your timer does not run.
You should put the code in the ViewDidLoad method instead.
LikeLike
Man Thank you , It’s work perfect 🙂
LikeLike
you are welcome~ =)
LikeLike
impressive work, thanks
LikeLike
You are welcome. =)
LikeLike
Great help, thanks.
LikeLike
Thanks for your comment and good to know this post could help. =)
LikeLike
Hello! I’m new to iOS. Can I know how could I implement this to refresh Banners every X seconds?
LikeLike
Hello! I am new to iOS. Can you help me please, how to use this code to Refresh Banners every X seconds? Thanks in advance.
LikeLike