NSTimer not repeating from AppDelegate - iphone

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

Related

How to create a NSTimer in Iphonesdk

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];

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 with method that have variable

I have a method that have input variable and I need to schedule this method usingNSTimer
Unfortunately when I try to make the idea I got some error
My code is the following:
My method:
-(void)movelabel:(UILabel *)label {
}
I make my scheduling using the following:
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(movelabel:myLbabeName) userInfo:nil repeats:YES];
But, I got the following error:
error: expected ':' before ')' token
In other case (case of method without input variable i'm calling the timer like the following:
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(myMethodNameWithoutVariable) userInfo:nil repeats:YES];
Regards
The selector you give to scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: does not take arbitrary arguments. It should be either a selector without parameter or a selector with a single parameter of type (NSTimer *).
That means you can't directly call moveLabel: with your parameter myLbabeName.
You could use the userInfo dictionary with an intermediary method like this:
(timerRef is a NSTimer class variable)
timerRef = [NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:#selector(timerMovelabel:)
userInfo:[NSDictionary dictionaryWithObject:myLbabeName
forKey:#"name"]
repeats:YES];
and
- (void)timerMovelabel:(NSTimer *)timer {
[self movelabel:[[timer userInfo] objectForKey:#"name"]];
}
EDIT
If you want to stop the timer, keep a reference to it and call [timerRef invalidate]
You need to add ':' after the parameter, i.e.
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(movelabel:myLbabeName:) userInfo:nil repeats:YES];
You can't pass ur lable as parameter with selector...There should be either one parameter which will be id or no parameter..
here you have to use
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(movelabel:) userInfo:nil repeats:YES];
or
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(movelabel) userInfo:nil repeats:YES];
if you use the first on then you can get you timer and action defination will be like
- (void) moveLable :(id)sender {
}
sender will be timer.
anyways why you need your lable as parameter. you can directly access your lable if you declare it in .h file.

Calling Instance Method from the Objective C Selector

Is it possible to call the instance method of an object from the selector? Is the below possible? It gives me compilation error that ":" is missing or something:
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(([self.person dance]))
userInfo:nil
repeats:YES];
Change target to self.person and use the dance selector:
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self.person
selector:#selector(dance)
userInfo:nil
repeats:YES];
Have you tried changing the target? Like this:
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self.person
selector:#selector(dance)
userInfo:nil
repeats:YES];