Cant get my NSTimer to end when the value <= 0 - ios

i cant seem to get my NSTimer to stop when it reaches 0 i've looked around for an answer and i tryed everything please help.
-(void)count {
mainint -= 1;
seconds.text = [NSString stringWithFormat:#"%i", mainint];
}
-(IBAction)start:(id)sender {
play.hidden = YES;
mainint = 60;
timer1 = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(count) userInfo:nil repeats:YES];
if (timer1 == 0) {
[timer1 invalidate];
timer1 = nil;
}
}

put your if condition in count() function
-(void)count {
mainint -= 1;
seconds.text = [NSString stringWithFormat:#"%i", mainint];
if (mainint == 0) {
[timer1 invalidate];
}
}

The issue is that your timer will never be nil (0) unless you set it yourself to nil. I think what you're trying to achieve is something like this :
-(void)count {
mainint -= 1;
seconds.text = [NSString stringWithFormat:#"%i", mainint];
if (mainint <= 0) {
[timer1 invalidate];
timer1 = nil;
}
}

Related

Enabling and diasbling button if statement xcode

I am looking to enable and disable a UIButton in XCode 7 using objective C. This is my code:
- (IBAction)startCount:(UIButton*)sender
{
countInt = 0;
self.Label.text = [NSString stringWithFormat:#"%i", countInt];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(countTimer) userInfo:nil repeats:YES];
if (countInt > 0){
sender.enabled = false;
} else {
sender.enabled = true;
}
}
Any idea as to why when I run this, the button doesn't enable and disable when desired?
If you want to disable the button when countInt is greater then 0, you have to move the code that does it to countTimer function.
-(void)countTimer {
if (countInt > 0){
myButton.enabled = false;
} else {
myButton.enabled = true;
}
countInt += 1;
self.Label.text = [NSString stringWithFormat:#"%i", countInt];
}
But there's other ways to do this more efficiently.

Keep label updating through segue

I have a small sample project i'm using to figure out how to implement this on my main project. This simple project has 2 VC's both with segues to each other.
On the initial VC is a button which leads to the TimerVC (both using the same class).
The TimerVC has a button and a label. When the button is pressed the label will increase by 1 every second.
If the timer is on and I segue back to the initial VC and then to the TimerVC the timer continues but the label stops updating.
How can I keep the label updating? The timer keeps going in the back-end but once the segue happens the label stops updating.
EDIT: Code provided below. The Timer is also more complex to represent a little of what I'm trying to do.
VC.H
NSTimer * timer;
NSTimer * updateTimer;
#property (weak, nonatomic) IBOutlet UIButton *idleOutler;
- (IBAction)idleAttack:(id)sender;
VC.M
int enemy001Hp = 100;
int deadEnemy = NO;
int noHealth = 0;
bool enemy001Active = NO;
- (void) enemy1 {
enemy001Active = YES;
self.enemyHpLabel.text = [NSString stringWithFormat:#"%i", enemy001Hp];
}
- (void) enemyDamageTimer {
if (enemy001Active == YES) {
enemy001Hp -= 50;
self.enemyHpLabel.text = [NSString stringWithFormat:#"%i",enemy001Hp];
}
}
- (void) updateLabelTimer {
if (enemy001Active == YES) {
self.enemyHpLabel.text = [NSString stringWithFormat:#"%i", enemy001Hp];
}
}
- (void) stopTimer {
[timer invalidate];
timer = nil;
self.enemyHpLabel.text = [NSString stringWithFormat:#"0"];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self enemy1];
if (enemy001Active) {
self.enemyHpLabel.text = [NSString stringWithFormat:#"%i", enemy001Hp];
} else if (enemy002Active == YES) {
self.enemyHpLabel.text = [NSString stringWithFormat:#"%i", enemy002Hp];
}
}
- (IBAction)idleAttack:(id)sender {
idleOn = YES;
self.idleOutler.hidden = YES;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(enemyDamageTimer) userInfo:nil repeats:YES];
updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(updateLabelTimer) userInfo:nil repeats:YES];
if (enemy001Active == YES) {
if (enemy001Hp <= 0) {
[self enemy2];
enemy001Active = NO;
enemy002Active = YES;
}
} else if (enemy002Active == YES) {
if (enemy002Hp <= 0) {
self.enemyHpLabel.text = [NSString stringWithFormat:#"0"];
enemy002Hp = 0;
enemy002Active = NO;
[self stopTimer];
}
}
}

Calculate current time from next minitue then NSTimer should not work

hi i am kind of new to iOS.Now i have confused with this concept..the concept is that i want to get current time(HH:MM:SS)when i click the button.Inside the button i have done something repeating task by using NSTimer.once i get the current time after one minitue the NSTimer sholud invalidate(the nstimer is call each(0.05f)).Here i need to calculate pressing time with next one minitue.thanks in advance.sorry dont mistake me i am stupid in english.
here is the code i am still now..
- (void) scanButtonTouchUpInside {
if (check) {
position=CGPointMake(14.0f, 7.0f);
position1=CGPointMake(7.0f, 14.0f);
check=NO;
timer= [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:#selector(onTimer) userInfo:nil repeats:YES];
}
else
{
//animation stop code here.................
if ([timer isValid]) {
[timer invalidate];
}
check=YES;
NSLog(#"stopppppppp.....................");
// overlayGraphicView.frame = CGRectMake(30, 100, 32, 32);
// overlayGraphicView1.frame = CGRectMake(30, 152, 32, 32);
}
}
-(void)onTimer
{
overlayGraphicView.center = CGPointMake(overlayGraphicView.center.x+position.x,overlayGraphicView.center.y+position.y);
if(overlayGraphicView.center.x > 320 || overlayGraphicView.center.x < 0)
position.x = -position.x;
if(overlayGraphicView.center.y > 480 || overlayGraphicView.center.y < 0)
position.y = -position.y;
overlayGraphicView1.center = CGPointMake(overlayGraphicView1.center.x+position1.x,overlayGraphicView1.center.y+position1.y);
if(overlayGraphicView1.center.x > 320 || overlayGraphicView1.center.x < 0)
position1.x = -position1.x;
if(overlayGraphicView1.center.y > 480 || overlayGraphicView1.center.y < 0)
position1.y = -position1.y;
}
First scanButtonTouchUpInside will run once because you start the timer there
timer= [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:#selector(onTimer) userInfo:nil repeats:YES];
And when the method onTimer run, nothing call scanButtonTouchUpInside again, so you need add [self scanButtonTouchUpInside]; at the end.
Second, to calculate the you need to do this
-(NSInteger)calculateMinutesFromLastDate:(NSDate *)lastDate toNow:(NSDate *)nowDate
NSDateComponents *nowDateComponents = [[NSCalendar currentCalendar] components:NSMinuteCalendarUnit fromDate:nowDate];
NSInteger nowCurrentMinute = [nowDateComponents minute];
NSDateComponents *lastDateComponents = [[NSCalendar currentCalendar] components:NSMinuteCalendarUnit fromDate:lastDate];
NSInteger lastMinute = [lastDateComponents minute];
NSInteger differenceMinutes = nowCurrentMinute - lastMinute;
return differenceMinutes
}
Hope it help you.
i have done this simple way...
-(void)onTimer
{
overlayGraphicView.center = CGPointMake(overlayGraphicView.center.x+position.x,overlayGraphicView.center.y+position.y);
if(overlayGraphicView.center.x > 320 || overlayGraphicView.center.x < 0)
position.x = -position.x;
if(overlayGraphicView.center.y > 480 || overlayGraphicView.center.y < 0)
position.y = -position.y;
overlayGraphicView1.center = CGPointMake(overlayGraphicView1.center.x+position1.x,overlayGraphicView1.center.y+position1.y);
if(overlayGraphicView1.center.x > 320 || overlayGraphicView1.center.x < 0)
position1.x = -position1.x;
if(overlayGraphicView1.center.y > 480 || overlayGraphicView1.center.y < 0)
position1.y = -position1.y;
}
- (void) scanButtonTouchUpInside {
[self performSelector:#selector(timerInvalidate) withObject:nil afterDelay:60.0];
if (check) {
position=CGPointMake(14.0f, 7.0f);
position1=CGPointMake(7.0f, 14.0f);
timer= [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:#selector(onTimer) userInfo:nil repeats:YES];
}
}
-(void)timerInvalidate
{
if ([timer isValid]) {
[timer invalidate];
}
}
The overlay source taken from https://github.com/jj0b/OverlayViewTester

NSTimer Milliseconds countdown

I would like to make a simple countdown with seconds and millisecond: SS:MM.
However, i would like to stop the timer or do something when the timer reach 0:00.
Currently the timer works, but it doesnt stop at 0:00. I can make the seconds stop, but not the milliseconds. What is wrong?
-(void) setTimer {
MySingletonCenter *tmp = [MySingletonCenter sharedSingleton];
tmp.milisecondsCount = 99;
tmp.secondsCount = 2;
tmp.countdownTimerGame = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:#selector(timerRun) userInfo:nil repeats:YES];
}
-(void) timerRun {
MySingletonCenter *tmp = [MySingletonCenter sharedSingleton];
tmp.milisecondsCount = tmp.milisecondsCount - 1;
if(tmp.milisecondsCount == 0){
tmp.secondsCount -= 1;
if (tmp.secondsCount == 0){
//Stuff for when the timer reaches 0
//Also, are you sure you want to do [self setTimer] again
//before checking if there are any lives left?
[tmp.countdownTimerGame invalidate];
tmp.countdownTimerGame = nil;
tmp.lives = tmp.lives - 1;
NSString *newLivesOutput = [NSString stringWithFormat:#"%d", tmp.lives];
livesLabel.text = newLivesOutput;
if (tmp.lives == 0) {
[self performSelector:#selector(stopped) withObject:nil];
}
else {[self setTimer]; }
}
else
tmp.milisecondsCount = 99;
}
NSString *timerOutput = [NSString stringWithFormat:#"%2d:%2d", tmp.secondsCount, tmp.milisecondsCount];
timeLabel.text = timerOutput;
}
-(void) stopped {
NSLog(#"Stopped game");
timeLabel.text = #"0:00";
}
Well. You do
tmp.milisecondsCount = tmp.milisecondsCount - 1;
if(tmp.milisecondsCount == 0){
tmp.milisecondsCount = 100;
tmp.secondsCount -= 1;
}
And right after that
if ((tmp.secondsCount == 0) && tmp.milisecondsCount == 0) {
//stuff
}
How could it ever happen that they're both 0 if, as soon as milisecond reaches 0, you reset it to 100?
EDIT: Do instead something like:
if(tmp.milisecondsCount < 0){
tmp.secondsCount -= 1;
if (tmp.secondsCount == 0){
//Stuff for when the timer reaches 0
//Also, are you sure you want to do [self setTimer] again
//before checking if there are any lives left?
}
else
tmp.milisecondsCount = 99;
}
In your code, a first condition is met
if(tmp.milisecondsCount == 0){
tmp.milisecondsCount = 100;
so that the next conditional statment
&& tmp.milisecondsCount == 0
will never be true.

iOS Label Text Based On Timed Function

I've been trying to create a script that sets a timer and reduces a value by 33 each time it runs. In theory the timer should stop itself after 6 runs, but it keeps going and reaches negative values. Is there a reason for this?
-(void)checkValue:(NSTimer *)myTimer{
if(pplint > 0){
pplint = pplint-33;
NSString *ppllefttext = [NSString stringWithFormat:#"%d", pplint];
peopleleft.text = ppllefttext;
NSLog(#"Reduced Timer");
}
if(pplint < 0){
NSLog(#"Stop Timer");
[myTimer invalidate];
}
}
- (IBAction)gotocongrats{
pplint = 198;
NSString *ppllefttext = [NSString stringWithFormat:#"%d", pplint];
peopleleft.text = ppllefttext;
NSTimer * myTimer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:#selector(checkValue:)
userInfo:nil
repeats:YES];
NSLog(#"Started Timer");
}
I took you code and edited it a bit and it works just fine for me.
-(void)checkValue:(NSTimer *)myTimer{
if(pplint > 0){
pplint = pplint-33;
NSLog(#"%i",pplint);
NSLog(#"Reduced Timer");
}
if(pplint <= 0){
NSLog(#"Stop Timer");
[myTimer invalidate];
}
}
- (IBAction)gotocongrats{
pplint = 198;
NSTimer * myTimer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:#selector(checkValue:)
userInfo:nil
repeats:YES];
NSLog(#"Started Timer");
}

Resources