I have the following method
-(void) showDie: (int) num;{
// do something
}
I want to call that method in time intervals of .5 secs. So far I have the following
SEL runShowDie;
[runShowDie performSelector:#selector(showDie:) withObject:rand];
[NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:runShowDie userInfo:nil repeats:NO];
What am I doing wrong?
You put repeats:No in this line make it YES
[NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:#selector(showDie:) userInfo:[NSString stringWithFormat:#"%d",rand] repeats:YES];
Try with
[self performSelector:#selector(showDie:) withObject: rand afterDelay:0.5f];
And remove NSTimer
Related
i have more than 10 objects of the same object type, lets call it Car type.
in my class, for each object i need to start a NSTimer.
//for object 1:
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(timerTick:) userInfo:nil repeats:YES];
//for object n:
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(timerTick:) userInfo:nil repeats:YES];
[EDIT] the timers are fired separately not at the same time.
However i have only one method for the 10 objects:
- (void)timerTick:(NSTimer *)timer
{
//here i need the reference to the object x (car) in order to display the timer and the name of the car for instance.
}
how can i achieve this, please?
im thinking of subclassing NSTimer and adding a new property (car) to it.
CustomTimer *mytimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(timerTick:) userInfo:nil repeats:YES];
- (void)timerTick:(CustomTimer *)timer
{
NSLog(#"%#", timer.car.name);
}
thanks
I want to my Webservice invoke once every 6 Hours.I am newer in iOS. Please help any help would be apperciated.I am stuck.
You can user NSTimer and schedule it for 6 hours
NSTimer *timer = nil;
timer = [NSTimer scheduledTimerWithTimeInterval:6*60*60 //6 hour
target:self
selector:#selector(performAction)
userInfo:nil
repeats:YES];
you can call using NSTimer. this is automatically called which time you set.
e.g.
NSTimer *timer= [NSTimer scheduledTimerWithTimeInterval:10.0(your time) target:self selector:#selector(someMethod) userInfo:nil repeats:YES];
-(void)someMethod
{
////API called here...
}
In ViewDidLoad
NSTimer *timer = [NSTimer timerWithTimeInterval:360.0 target:self selector:#selector(hideandview) userInfo:nil repeats:YES];
-(void)hideandview
{
////API called here...
}
You should save the last time in prefence (NSUserDefaults) when you make a call.
Whenever the app starts. Start timer and check current time with last saved time and get the difference.
Im making an iphone game that uses NStimer for movement. I found out that there is a bug that makes the timers stack some times, but i havnt found out what causes is. Is there any way to see how many timers are allocated and is there any way to prevent it by doing something like this:
If(myTimer.numberOfAllocatedTimers == 0) {
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:#selector(updateme) userInfo:nil repeats:YES];
}
Try this:
// use this to start timer
myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(doSthing) userInfo:nil repeats:YES];
// use this to stop timer
if (myTimer) {
[myTimer invalidate];
myTimer = nil;
}
A little more context on where this code is being used would be helpful.
However, I'll try to explain why it's happening anyway.
Every time you use [NSTimer scheduledTimerWithTimeInterval...], a new timer is created, and will start repeating. If your code creates a timer inside the callback method, see example below, you'll stack timers.
- (void)updateme {
if ([NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:#selector(updateme) userInfo:nil repeats:YES]) {
// This creates stacked timers!
}
}
Here's how you should do it:
#property (nonatomic, strong) NSTimer *myTimer;
...
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:#selector(updateme:) userInfo:nil repeats:YES];
...
- (void)updateme:(NSTimer *)timer {
if (timer == self.myTimer) {
// Do something because the timers are equal
}
}
I have gone through many sites but still no answer.
I have a method suppose void xyz(), which will get called automatically from a View Controller after every 3 seconds.
I have no idea what to use, do I have to use NSThread or PerformSelector.
Call this method from ViewDidLoad method.ViewDidLoad will when your view will be appear in iPhone device or Simulator.
[NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:#selector(runMethod) userInfo:nil repeats:YES];
-(void)runMethod
{
}
Something like this
-(void)xyz{
[self performSelectorInBackground:#selector(xyz) withObject:nil];
}
- (void)viewDidLoad {
[self performSelector:#selector(xyz) withObject:nil afterDelay:0.3];
}
Use NSTimer
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:#selector(xyz) userInfo:nil repeats:YES];
You should use NSTimer as mentioned by #mokujin.
Please visit https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
NSTimer doesn't stop
I have this code:
[NSTimer scheduledTimerWithTimeInterval:110.0
target:self
selector:#selector(targetMethod:)
userInfo:nil
repeats:YES];
- (void) targetMethod:(NSTimer) timer{
NSLog(#"Hello World");}
in targetMethod I can stop timer with [timer invalidate], but out of this method, How can I stop targetMethod?
You can keep your NSTimer in a variable and stop the timer using the invalidate method. Like the following:
NSTimer * myTimer = [NSTimer scheduledTimerWithTimeInterval:110.0
target:self
selector:#selector(targetMethod:)
userInfo:nil
repeats:YES];
[myTimer invalidate];
One way to do it is to create NSTimer *timer; as a global variable so you can have a handle to the timer. Some thing likes this:
NSTimer *timer; //global var
timer = [NSTimer scheduledTimerWithTimeInterval:110.0
target:self
selector:#selector(targetMethod:)
userInfo:nil
repeats:YES];
To stop timer somewhere in the same class:
[timer invalidate];