How to create a NSTimer in Iphonesdk - iphone

I am new to Iphone programming I want to know about how to create a NSTimer in Xcode Iphonesdk. Can anyone suggest me?

NSTimer *timer = [NSTimer scheduleTimerWithTimeInterval:TIME_INTERVAL target:self selector:#selector(YOUR_METHOD_NAME) userInfo:nil repeats:YES/NO];
Hope this will help you........

Here is a code to add NSTimer..
1) in .h file
NSTimer *timer;
2) in .m file
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(YOUR_METHOD_NAME) userInfo:nil repeats:YES];
Note:: so your method will continuously call after every 1 second.. and if you want to call the timer method only once set repeats:NO...
To Invalidate Timer
3) to invalidate the timer where you want..
[timer invalidate];

Related

NSTimer and UITabBar

I have three tabbar items in one View. There is one NSTimer Schedule on Start Button Action in first tab item.When i press another tab item and come to the recent tab item(i.e tab item with NSTimer), it increases the seconds with 2. If i do the same for second time it increases the second with 3 and so on.I want seconds to be increased by 1.
This is the code i am using to schedule NSTimer
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(timerTick:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
I have tried to solve it using [timer invalidate]; but it is not giving me a proper result.
Thanks in advance.
in the - (void)viewWillAppear:(BOOL)animated
method make timer invalidate by [timer invalidate];
and again start it by :
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(timerTick:) userInfo:nil repeats:YES];

NSTimer not repeating from AppDelegate

why would this not repeat when place in appDidFinishLaunching?
self.ti = [NSTimer timerWithTimeInterval:10. target:self selector:#selector(bounce:) userInfo:nil repeats:YES];
[self.ti fire];
many thanks
Jules
I think your bounce has a wrong signature. It should be
- (void)bounce:(NSTimer*)theTimer {
NSLog(#"Here...");
}
You should be using selector(bounce:) to schedule this method. You should also be calling scheduledTimerWithTimeInterval instead of timerWithTimeInterval:
self.ti = [NSTimer
scheduledTimerWithTimeInterval:10.
target:self
selector:#selector(bounce:)
userInfo:nil
repeats:YES];
I'm not sure if it will help, but try using scheduledTimerWithTimeInterval method. An example:
self.ti = [NSTimer scheduledTimerWithTimeInterval:10. target:self selector:#selector(bounce) userInfo:nil repeats:YES];
Hope it helps

Stopping a 'performSelector afterDelay' before it fires

I start a repeating NSTimer after a 4 second delay using the following code:
- (void)viewDidLoad {
[self performSelector:#selector(startTimer) withObject:self afterDelay:4];
}
- (void)startTimer {
NSTimer *mytimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(doSomething) userInfo:nil repeats:YES];
}
- (void)doSomething {
NSLog(#"What up!");
}
Problem is I may need to cancel startTimer from being called before the 4 seconds is up. Is there a way of doing this? I'd actually prefer to not use the performSelector in the first place (seems messy). If only NSTimer had something along the lines of this…
NSTimer *mytimer = [NSTimer
scheduledTimerWithTimeInterval:1.0
afterDelay:4.0 target:self
selector:#selector(doSomething)
userInfo:nil repeats:YES];
…then that would be perfect as I could just call the following:
[myTimer invalidate];
Any help or tips are much appreciated =)
P.S. I've found something called cancelPreviousPerformRequestsWithTarget in the NSObject class reference. Doesn't seem to be a method I can call from where this code runs however. If that's getting back on the right track your feedback is welcome!
Plz go through the SP post link
Stopping a performSelector: from being performed
[NSObject cancelPreviousPerformRequestsWithTarget:self
selector:#selector(sr)
object:nil];
The documentation for -performSelector:withObject:afterDelay: points you to the methods for canceling a queued perform request.
[myTimer invalidate] doesn't work?
Just keep a track of the object in your class, or in a centralized store for example.
If you do so, you could access your timer from everywhere you want, and invalidate it whenever it is needed
Use the NSTimer to fix issue.
self.autoTimer = [NSTimer timerWithTimeInterval:3.0 target:self
selector:#selector(connectionTimeout:) userInfo:nil repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:autoTimer
forMode:NSDefaultRunLoopMode];
and call when you want to stop timer
[self.autoTimer invalidate];
self.autoTimer = nil;

Problem with NSTimer called inside my -parserDidEndDocument

I have a NSTimer object.
timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:#selector(timerAction) userInfo:nil repeats:YES];
[timer fire];
The method 'timerAction' repeats perfectly when call the timer from viewDidLoad method, but when I call the timer from parserDidEndDocument, the method 'timerAction' runs only once. Why is this?
you can try running the timer on the main thread.
Try this
create a new method that includes the code, to start timer, like :-
-(void)createTimer{
timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:#selector(timerAction) userInfo:nil repeats:YES];
[timer fire];
}
In Your parserDidEndDocument delegate, try this:
[self performSelectorOnMainThread:#selector(createTimer) withObject:[nil waitUntilDone:YES]

NSTimer stopping in run time?

how can i stop NSTimer in runTime?
I am using the following code .but NSTimer runs again and again.(i want to repeat NStimer, i want to stop in runtime )
- (void)TimerCallback
{
.....
[self.tim invalidate];
self.tim = nil;
}
-(void)timerStart
{
self.tim = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(TimerCallback) userInfo:nil repeats:YES];
}
It's repeats:NO if you want it to run only once. You have repeats:YES.
It is [tim invalidate] and not self.tim invalidate
Do not do self.tim = nil, because that is releasing it. invalidate does everything.
For the record, make sure your property is all correct, ie
#property (nonatomic, retain) NSTimer *happyTimer;
and
#synthesize happyTimer;
For the record, you must be on the same thread.
Hope it helps.
Here is all the lines of code cut from a working production example:
NSTimer *ttt;
#property (nonatomic, retain) NSTimer *ttt;
#synthesize ttt;
self.ttt = [NSTimer scheduledTimerWithTimeInterval:7.00
target:self selector:#selector(ringBell) userInfo:nil repeats:YES];
if ( [ttt isValid] )
[ttt invalidate];
[ttt release]; // in dealloc
You need to add some debugging lines NSLog(#"I just invalidated"); and so on, to make sure you don't have some basic mistake.
Your code seems correct. Usually this problem is starting twice the timer. You can try
-(void)timerStart {
[self.tim invalidate];
self.tim = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(TimerCallback) userInfo:nil repeats:YES];
}