Calling a NSTimer method - ios

I'm new to working with timers on the iPhone and would need some support.
I have a method as below that takes the time and update a UILabel in my userinterface. I also have an NSTimer that calls that method once a second (I only show hours and minutes). This works just fine, except for the first second the App is live (as I understand it takes one second before the timer calls the method).
I would like to call my method from viewDidLoad, but how can I supply the right arguments? Or is there a better way of doing this?
// Timer for updating time
[NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:#selector(updateTime:)
userInfo:nil
repeats:YES];
-(void)updateTime: (NSTimer *) timer {
currentTime = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *currentTimeString = [dateFormatter stringFromDate:currentTime];
[dateFormatter release];
timeLabel.text = currentTimeString;
}
Appreciate anything that would point me in the right direction.

In your method, you don't use the timer variable right? So, why just call [self updateTime:nil]. That should work

One more option ... you can do:
[NSTimer scheduledTimerWithTimeInterval:0.01f
target:self
selector:#selector(updateTime:)
userInfo:nil
repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:#selector(updateTime:)
userInfo:nil
repeats: YES];

Related

WebService Call after timeInterval

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.

Tick timer in Objective C / iPhone? [duplicate]

This question already has answers here:
How do I use NSTimer?
(6 answers)
Closed 9 years ago.
I am making a Simon Says app to learn more about Objective C.
My SimonSaysViewController has 4 buttons. Their image needs to change accordingly when the pattern is being shown to the user.
A fixed interval timer will be absolutely fine.
I just cannot seem to find an example.
I basically would want a sort of setup like:
TimerTicked callback when I can do the image swapping logic.
Ideally, the TimerTicked method would be a method of my SimonSaysViewController.
How would this be done?
Thanks
NSTimer is your friend! Add an NSTimer property to your SimonSaysViewController.
#property (strong, nonatomic) NSTimer *tickTockTimer;
Depending on when you want the timer to start, you'll want to set up the timer then. Say you wanted the timer to start when the view first appears:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.tickTockTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(timerFired:) userInfo:nil repeats:YES];
}
Then implement the timerFired method and do what you need there.
- (void)timerFired:(NSTimer *)timer {
//change the image.
}
Don't forget to invalidate the timer when you are done.
- (void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.timer invalidate];
self.timer = nil;
}
This kind of thing usually works for me
NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:2.0]; // 2 sec from now
NSTimer *self.timer = [[NSTimer alloc] initWithFireDate:fireDate interval:5 target:self selector:#selector(timerDidTick) userInfo:nil repeats:YES]; // fire 5 sec apart
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:self.timer forMode:NSDefaultRunLoopMode];

How to stop NSTimer at a specific point?

I've created a simple button game that gives the user a point with every tap of the button. The button randomly appears on screen every 1.5 seconds. I want the game to end after 30 seconds or after 20 random button pop ups. I've been using the code below to have the button randomly pop-up on the screen:
timer = [NSTimer scheduledTimerWithTimeInterval: 1.5 target:self
selector:#selector(moveButton:)
userInfo:nil
repeats:YES];
I've declared the timer in the header file:
NSTimer *timer;
#property (nonatomic, retain) NSTimer *timer;
I've read Apple Docs on Using Timers but fail to fully understand it. I thought maybe I could use:
- (void)countedTimerFireMethod:(NSTimer *)timer{
count ++;
if(count > 20){
[self.timer invalidate];
self.timer = nil;
But it does not work properly. What am I doing wrong? I'm new to objective-C so I'm not that familiar with how things work.
The problem is on your timer method you are passing moveButton method but in below method where you are stopping the timer that method name is different so try this:-
self.timer = [NSTimer
scheduledTimerWithTimeInterval: 1.5 target:self
selector:#selector(moveButton:)
userInfo:nil
repeats:YES];
//just change the method name below
- (void)moveButton:(NSTimer *)timer{
count ++;
if(count > 20){
[self.timer invalidate];
self.timer = nil;}
If you are using new version of Xcode then you don not need to declare
NSTimer *timer;
and when scheduling a timer you can use
self.timer = [NSTimer scheduledTimerWithTimeInterval: 1.5 target:self
selector:#selector(moveButton:)
userInfo:nil
repeats:YES]
instead of
timer = [NSTimer scheduledTimerWithTimeInterval: 1.5 target:self
selector:#selector(moveButton:)
userInfo:nil
repeats:YES]
You are using correct method to stop the timer i.e invalidate
You can also refer the link for more clarification.
Please let me know if you solve this problem through the above code.

NSTimer initWithFireDate not working properly

I use NSTimer to periodically download data in the background, when wifi is enabled.
However there is also the option to press a button that downloads the data manually. Therefore it is possible to enable and disable the autoupdater.
When a period is "interrupted" by diabling the autoupdate, I want to schedule the next update when the remaining time of this period after the point of time when enabling it is over.
Because NSTimer has no pause method, I use NSTimer's initWithFireDate method.
timer = [[NSTimer alloc] initWithFireDate:fireDate interval:updateFrequency target:self selector:#selector(timerTick:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
the fireDate is definitely after the given date:
NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:remainingWaitingTime];
despite that,the timer is scheduled after the timer's initialization. I am disabling the timer and calculating the remaining time the following way:
NSDate *lastFireDate = [timer fireDate];
NSDate *current = [NSDate date];
[timer invalidate];
double timeSinceLastFire = [current timeIntervalSinceDate:lastFireDate];
remainingWaitingTime = updateFrequency - timeSinceLastFire;
The calculated time (remainingWaitingTime) has a correct value.
What am I doing wrong?
Best Regards,
Fabian
You have to add your timer to the run loop by doing the following:
[[NSRunLoop currentRunLoop] addTimer: timer forMode: NSDefaultRunLoopMode];

NSTimer does not want to fire after fireDate is Passed

I have an NSTimer which fires on the current date at a specific time set by the user. For example 12:00 PM at 2012-06-22. When the user clicks a button it triggers the timer if the hour is equal to 12:00 PM. It is ok, but after this time passed the NSTimer triggers again when the user click the button. I do not want such behavior, so how can I do so the timer does not fire when the fire date has passed?
NSTimer *t = [[NSTimer alloc] initWithFireDate:fireDate
interval:0.0
target:self
selector:#selector(doTimerAction:)
userInfo:nil repeats:NO];
NSRunLoop *runner = [NSRunLoop currentRunLoop];
[runner addTimer:t forMode: NSDefaultRunLoopMode];
[t release];
Are you sure you need NSTimer for this, providing I've understood you couldn't you just do
-(IBAction)btnPush:(id)sender{
if (hourInput == currentHour){
[self performSelector:#selector(doAction)];
}
Hope this helps if no leave me a comment and ill try to help more.

Resources