I am having a mac app in which I want to update my label after some seconds.
I have used the following code with NSTimer.
[NSTimer scheduledTimerWithTimeInterval:1.0/30 target:self selector:#selector(count:) userInfo:nil repeats:YES];
float k = 0.0;
if (k >= 958.36)
{
[timer invalidate];
return;
}
NSString* string = [NSString stringWithFormat:#"%.2f", k];
NSLog(#"%#",string);
[lbl setStringValue:string];
k++;
When I am using this code the value increases like below.
1.00
2.00
3.00
4.00
5.00
It only updates the integer values but not the decimal numbers. It should work like 1.10,1.11,1.12...
I know there are lots of questions on this and solutions but didn't work for me.
Can anyone help me on this?
Thanks...
The use of k++ is the issue as that is incrementing k by 1.0.
Increment using a different value, for example:
k += 0.1f;
You're printing k, which you are increasing by 1 every time the timer fires. If you want to print 1.10,1.11,1.12 et.c. you need to use:
NSString* string = [NSString stringWithFormat:#"1.%2f", k+10];
if (k >= 958.36)
{
[timer invalidate];
return;
}
NSString* string = [NSString stringWithFormat:#"%.2f", k];
NSLog(#"%#",string);
[lbl setStringValue:string];
k += 0.01f;
Related
This question already has answers here:
iOS Format String into minutes and seconds
(7 answers)
Closed 8 years ago.
I am trying to get a timer to show the counter like this "00:00:00". Here is my current code. I have been trying to get it to work using the stringWithFormat which should be easy but I guess I will have to set up the formats separately. Do you guys have any idea on how to do this?
- (void)TimerCount {
CountNumber = CountNumber + 1;
TimerDisplay.text = [NSString stringWithFormat:#"Hour: %0*i", length, hour];
}
- (void)timerCount {
{
CountNumber = CountNumber + 1;
NSInteger seconds = CountNumber % 60;
NSInteger minutes = (CountNumber / 60) % 60;
NSInteger hours = (CountNumber / 3600);
TimerDisplay.text = [NSString stringWithFormat:#"%i:%02i:%02i", hours, minutes, seconds];
}
Try the above code.
And configure this method to be fired every second.
In viewDidLoad
NSTimer *counterTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(timerCount)
userInfo:nil
repeats:YES];
[[NSRunLoop mainRunLoop] addTimer: counterTimer forMode:NSRunLoopCommonModes];
And keep the counterTimer as an iVar to keep it alive until the VC is dealloced, if you are using ARC.
- (void)TimerCount
{
CountNumber++;
NSString *time = [[NSString alloc] init];
NSUInteger seconds = CountNumber;
NSUInteger minutes = 0;
NSUInteger hours = 0;
if (seconds > 59) {
seconds -= 60;
minutes++;
if (seconds < 10) {
time = [NSString stringWithFormat:#":0%i",seconds];
} else time = [NSString stringWithFormat:#":%i",seconds];
}
if (minutes > 59) {
minutes -= 60;
hours++;
if (minutes < 10) {
time = [NSString stringWithFormat:#":0%i%#",minutes,time];
} else time = [NSString stringWithFormat:#":%i%#",minutes,time];
}
if (hours < 10) {
time = [NSString stringWithFormat:#"0%i%#",hours,time];
} else time = [NSString stringWithFormat:#"%i%#",hours,time];
}
NSString *time is the time.
Also NSTimer to call this method every second:
NSTimer *counterTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(timerCount)
userInfo:nil
repeats:YES];
I'd like to show my countdown for a process to load, and understand that it is disallowed using ARC to convert an int to an NSString. How can I show my count within an NSString?
static int count = 0;
count++;
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:(5.0)
target:self
selector:#selector(uploadData)
userInfo:nil
repeats:NO];
if (count <= 5)
{
ilabel.text = #"Please be patient...";
NSString *counter = count;
counterLabel.text = counter;
}
Probably the easiest way to do this is:
NSString* counter = [NSString stringWithFormat: #"%d", count];
counterLabel.text = [NSString stringWithFormat:#"%d",count];
I have UITextfield that is set to show only 1 character upon initialization, however I use constraints to make the field be able to expand as needed.
My constraint:
#"H:[txtField(<=80)]-14-|
When I set the textField via the text attribute it resizes perfectly, however when I try and enter it in via the keyboard the best I can get is to use sizeToFit inside the shouldChangeCharactersInRange...which kind of works but instead of the dollar amount moving inward to the left on the iphone it moves outward to the right and off the screen.
How would I make it move inward to the left?
I ended up using this little bit I found on StackOverflow in the
shouldChangeCharactersInRange
double currentValue;
NSString *str;
if(![textField.text length]){
str = #"$ 0.00";
}else{
str = textField.text;
}
currentValue = [[str substringFromIndex:1] doubleValue];
double cents = round(currentValue * 100.0f);
if ([string length]) {
for (size_t i = 0; i < [string length]; i++) {
unichar c = [string characterAtIndex:i];
if (isnumber(c)) {
cents *= 10;
cents += c - '0';
}
}
} else {
// back Space
cents = floor(cents / 10);
}
textField.text = [NSString stringWithFormat:#"%.2f", cents / 100.0f];
//Add this line
[textField setText:[NSString stringWithFormat:#"$%#",[textField text]]];
return NO;
i create a countdown timer that go from 10 to 0. i create a uilabel that shows the counter in seconds. now i want the label to show the counter minutes and second, like that: 00:00.
how can i do that?
here is my countdowncode:
-(void)countdown
{
countdownCounter -= 1;
countdown.text = [NSString stringWithFormat:#"%i", countdownCounter];
}
-(IBAction)strat:(id)sender
{
countdownCounter = 10;
countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(countdown) userInfo:nil repeats:YES];
}
}
thanks!
This can be done using one time, and one label. Try using the following code:
int seconds = [countDown.text intValue] % 60;
int minutes = ([countDown.text intValue] / 60) % 60;
countDown.text = [NSString stringWithFormat:#"%2d:%02d", minutes, seconds];
Do it exactly the same way, just add another timer.
countdownTimer2 = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:#selector(countdown2) userInfo:nil repeats:YES];
-(void)countdown2
{
countdownCounterMinutes -= 1;
}
and change countdown to
-(void)countdown
{
countdownCounter -= 1;
countdown.text = [NSString stringWithFormat:#"%i%i", countdownCounterMinutes, countdownCounter];
}
for anyone who what the answer in the end i do it this way:
-(IBAction)start
{
timer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:#selector(updateTimer:) userInfo:nil repeats:YES];
}
-(void)updateTimer:(NSTimer *)timer {
currentTime -= 10 ;
[self populateLabelwithTime:currentTime];
if(currentTime <=0)
[timer invalidate];
}
- (void)populateLabelwithTime:(int)milliseconds {
seconds = milliseconds/1000;
minutes = seconds / 60;
hours = minutes / 60;
seconds -= minutes * 60;
minutes -= hours * 60;
NSString * result1 = [NSString stringWithFormat:#"%#%02d:%02d:%02d:%02d", (milliseconds<0?#"-":#""), hours, minutes, seconds,milliseconds%1000];
result.text = result1;
}
in viewDidLoad i set currentTime for the countdown time in milliseconds.
hope you understand...
I formatted the numbers (from float)with mins and seconds
it this way, thanks your answers helped. (hope this helps another)
- (void)ticTimer
{
self.current -= self.updateSpeed;
CGFloat progress = self.current / self.max;
[self populateLabelwithTimeFormatted:self.current];
/// TimeLabelNode.text = [NSString stringWithFormat:#"%f", progress];
// * Time is over
if (self.current <= self.min) {
[self stop];
_TimeLabelNode.text= #"time up!";
}
}
- (void)populateLabelwithTimeFormatted:(float)time {
//convert float into mins and seconds format
int mytime = (int) _current;
int seconds = mytime%60;
int minutes = mytime / 60 % 60;
NSString * result1 = [NSString stringWithFormat:#"%2d:%02d", minutes, seconds];
_TimeLabelNode.text = result1;
}
I'm trying to add values to the yArray. However in the NSLog, it always shows up null. I am synthesizing the NSMutableArray (yArray) and get no errors or warnings on compiling. I believe one reason might be that i am not initializing the array. I read somewhere that #synthesize does not initialize. However, i'm a beginner and i'm not sure how to properly initialize the array. I've researched it on google but couldn't figure it out. Can someone tell me how or at least point me in the right direction. I posted a part of my code below. Thank you in advance.
for (int i = 0; i < n; ++i)
{
xx = xx + [[self.yArr1 objectAtIndex:i] doubleValue];
}
for (int i = 0; i < n; ++i)
{
if(i==0)
[self.yArray addObject:[NSNumber numberWithDouble:(0.0)]];
else
{
bb = fabs([[self.yArr1 objectAtIndex:i] doubleValue]- [[self.yArr1 objectAtIndex:(i-1)] doubleValue]);
[self.yArray addObject:[NSNumber numberWithDouble:(bb)]]; // This is the problem.
yy = yy + [[self.yArray objectAtIndex:i] doubleValue];
}
NSLog(#"prediction %f, %f, %#, %f", bb, yy, [self.yArray objectAtIndex:i],[[self.yArr1 objectAtIndex:i] doubleValue]);
}
NSLog(#"prediction %f, %f", bb, yy);
To properly initialize the array use
NSMutableArray* yArray = [[NSMutableArray alloc] init];
Initialize it before using it (maybe in init method or something like that), then retry. Don't forget to manually release it when you have done.
Just initialize it first in a location that will be hit prior to your addObject code. e.g.
yArray = [[NSMutableArray alloc] init];