How do I link the iphones time to change a UILabel - iphone

I want to link the time on the iphone such as midnight to change the text of a UILabel to something else anyone help out?
Ive looked through the NSTimer and found nothing that works any help?

Try this:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(update:) userInfo:nil repeats:YES];
And...
- (void) update:(NSTimer *) timer {
myLabel.text = [NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle: NSDateFormatterLongStyle timeStyle: NSDateFormatterLongStyle];
}

There are two other ways: use -[performSelector:withObject:aterDelay:] (and NSObject has a class method to cancel such, or better,
your appDelegate can implement:
-(void)applicationSignificantTimeChange:(UIApplication *)app;
which will get called at midnight, carrier time update, and daylight savings time change

Related

creating a timer for a level in iphone game

Im trying to add a timer to my game so that the user knows how long they have spent playing a level. Ive figured out that I can initialize a timer the following way:
bool showTimer = YES;
NSDate startDate;
UILabel timerLabel; // initialized in viewDidLoad
-(void) showElapsedTime: (NSTimer *) timer {
if (showTimer) {
NSTimeInterval timeSinceStart;
if(!startDate) {
startDate = [NSDate date];
}
timeSinceStart = [[NSDate date] timeIntervalSinceDate:startDate];
NSString *intervalString = [NSString stringWithFormat:#"%.0f",timeSinceStart];
timerLabel.text = intervalString;
if(stopTimer) {//base case
[timer invalidate];
}
}
}
- (void) startPolling {
[NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:#selector(showElapsedTime:) userInfo:nil repeats:YES];
}
I start the startPolling method in the viewDidLoad. When I run the app, I do see the timer and it tracks the time but when I exit the app and re-enter it, the timer doesnt pause. I'm also not sure how to handle going to another view (like the options menu) and then coming back to this view. I understand NSDefaults and NSCoding and I see how I could save the current value on the timer as a Coding object, keeping a seperate key-value pair in a plist for every level but this seems cumbersome.
Is there a better way to keep track of how long the user spends in a level?
Instead of doing the calculation (subtracting the start time from the current time) every time, since all you care about is an elapsed time, just have a variable like NSTimeInterval elapsedTime that you start at 0 and add time to every time that the timer fires. (If you want to track it to 0.1 seconds like in your example, just divide by 10 before displaying it.) This way, you can pause it whenever you want and it will just continue on from where it was before when it starts up again.

TimeInterval function setting with UIPicker

I have following part of code. It is executing when user will select any time interval from UIpicker.
if([[arrayNo objectAtIndex:row] isEqualToString:#"1 minutes"])
time=60;
if([[arrayNo objectAtIndex:row] isEqualToString:#"5 minutes"])
time=300;
[NSTimer scheduledTimerWithTimeInterval:60 target:self selector:#selector(updateMethod) userInfo:nil repeats:YES];
I am defining two time in UIPicker "1 minutes" and "5 minutes". Suppose once user will select "1 minutes" then function "updateMethod" will be call after 1 minutes of time period. But let suppose user again change his time to "5 mnutes" from UIPicker. Then what will happen? Is timer will be set to "5 minutes" OR it will be set to "1 minutes" and "5 minutes" both?
How will I design code to set function calling for one time only?
Help me in it concept?
Every time this is called, you will schedule a new timer, so in your example, you will get both.
I generally discourage scheduledTimerWithTimeInterval:... for basically this reason. You now have no way to cancel your timer. You should create an NSTimer* ivar like this:
#interface ... { }
#property (nonatomic, readwrite, retain) NSTimer *updateTimer;
#end
#implementation ...
#synthesize updateTimer=updateTimer_;
- (void)setUpdateTimer:(NSTimer *)aTimer {
if (aTimer != updateTimer_) {
[aTimer retain];
[updateTimer_ invalidate];
[updateTimer_ release];
updateTimer_ = aTimer;
}
}
...
self.timer = [NSTimer timerWithTimenterval:60 target:self selector:#selector(updateMethod) userInfo:nil repeats:YES];
This will automatically clear the old timer whenever you set the new timer.
Do note that basing logic on UI strings (#"1 minutes") will break localization if you ever want this to be translated into other languages. Instead, you would normally either just use row to decide the value, or you would add small dictionaries or objects to arrayNo that would hold the label and the value.

running a time interval computation in the background

I have a NSTimeInterval which is basically
NSTimeInterval interval = [date1 timeIntervalSinceDate: [NSDate date]];
I want this computation to be always running in the background when I am in a view X, so I can display a timer counting down in a UILabel.. how do I do this? It's like in the groupon iphone app, but it doesn't show it in details of seconds
You could use an NSTimer to get a method called at a set time interval (however this is all performed on the main thread, not on a background thread):
- (void)setupTimer {
//Start a timer to call update updateInterval: every 1 second
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(updateInterval:)
userInfo:nil
repeats:YES];
}
- (void)updateInterval:(NSTimer *)timer {
NSTimeInterval interval = [date1 timeIntervaleSinceDate:[NSDate date]];
//Do other things like updating the label
}

Ios UILabel update performance

in my application i have an UILabel showing a time updated every second.
I have also draggable objects in the screen.
When I hide the label or i stop the timer everything is perfect but when i start the timer the animation performance of dragging objects go down.
I put the UILabel updating in a separate thread but no luck.
I need help friends :)
This is my code:
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(onTimer:) userInfo:nil repeats:YES];
-(void) onTimer:(NSTimer *)timer;
{
timeInterval ++;
int hours = (int)timeInterval / 3600;
int minutes = (timeInterval %3600)/ 60;
int seconds = ((timeInterval%3600)%60);
NSString *timeDiff = [NSString stringWithFormat:#"%d:%02d:%02d", hours,minutes,seconds];
[NSThread detachNewThreadSelector:#selector(setText:) toTarget:self.time withObject:timeDiff];
}

NSTimer problem

I am using this code:
timer = [NSTimer scheduledTimerWithTimeInterval:2
target:self
selector:#selector(update)
userInfo:nil
repeats:YES];
...
-(void)update {
NSDate *date = [NSDate date];
NSString *Currentdate = [date descriptionWithCalendarFormat:#"%b %d, %Y %I:%M:%S %p"
timeZone:nil
locale:nil];
lbl.text = Currentdate;
}
It works fine but I want to change timer interval from 2 seconds to 1 second at run time. How can I do this?
Thanks in advance.
You can't change a timer interval after it's been created.
// You have to invalidate it...be careful,
// this releases it and will crash if done to an already invalidated timer
if (self.timer != nil) {
[self.timer invalidate];
self.timer = nil;
//... then declare a new one with the different interval.
timer = [NSTimer scheduledTimerWithTimeInterval:newInterval
target:self
selector:#selector(update)
userInfo:nil
repeats:YES];
}
Just invalidate the old timer and create new. I don’t think NSTimer supports changing the time interval, it would be an extra cruft both in the interface and the implementation.