iOS NSTimer firing twice - ios

Hi I'm trying to use NSTimer to create a countdown which and view this using a label. the problem is the label goes down in two's and i don't know why. any ideas?
heres my code
- (void)viewDidLoad
{
NSLog(#"%#", self.chosenTime);
[self startGame];
[super viewDidLoad];
NSString *timeString = self.chosenTime;
self.timer = [timeString intValue];
self.countdown.text = timeString;
// Do any additional setup after loading the view.
}
- (void)startGame
{
[self introCountdown];
[self performSelector:#selector(goAnimation) withObject:nil afterDelay:3.0];
NSString *timeString = self.chosenTime;
self.timer = [timeString intValue];
[self performSelector:#selector(startCountdown) withObject:nil afterDelay:4.0];
}
- (void)startCountdown
{
//self.countdownTimer = [NSTimer timerWithTimeInterval:1 target:self selector:#selector(decrementTimer:) userInfo:nil repeats:YES];
self.countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.00 target:self selector:#selector(decrementTimer:) userInfo:nil repeats:YES];
}
- (void)decrementTimer:(NSTimer *)timer
{
if(self.timer > 0)
{
self.timer = self.timer - 1;
self.countdown.text = [NSString stringWithFormat:#"%d", self.timer];
}
else
{
[self.countdownTimer invalidate];
self.countdownTimer = nil;
}
}

This code seems to be correct. May be you are changing label's text from somewhere else?

Sorry for the misinterpretation, the code is correct, it seems a UI problem or handling label from somewhere else, attach your source with xib's if possible

Related

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];
}
}
}

NSTimer Countdown issue

Hello I recently added a timer in my app so when the user clicks the balloon it starts the timer but I also have a tap counter hooked to the button as well. So heres my issue when I press the balloon multiple times the tap counter works and so does the timer but when start to tap faster the timer becomes stuck in a loop and when I stop tapping it, it counts down past zero into negatives really fast
How do I fix this?
- (IBAction)yourbuttonClicked1:(UIButton *)sender
{
//start a background sound
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"tap" ofType:
#"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath ];
myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil ];
[myAudioPlayer play];
//for Switch view
ViewController2 *nextView = [[ViewController2 alloc] initWithNibName:#"ViewController"
bundle: nil];
[self.navigationController pushViewController:nextView animated:YES];
This is where the timer code is I think it might be conflicting with the tap counter?
myTime = 60;
countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self
selector:#selector(countDown) userInfo:nil repeats:YES];
}
-(void)countDown {
myTime = myTime;
countdownLabel.text = [NSString stringWithFormat:#"%i", myTime];
if (myTime == 0) {
[countdownTimer invalidate];
}
}
This is where the timer code ends!
- (void) saveNumberOfTaps:(int)numberOfTaps {
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithInt:numberOfTaps]
forKey:#"numberOfTaps"];
NSLog(#"Saved numberOfTaps: %i", numberOfTaps);
}
- (int) getNumberOfTaps {
if ([[[NSUserDefaults standardUserDefaults] objectForKey:#"numberOfTaps"] intValue]) {
int numberOfTaps = [[[NSUserDefaults standardUserDefaults]
objectForKey:#"numberOfTaps"] intValue];
NSLog(#"Getting numberOfTaps: %i", numberOfTaps);
return numberOfTaps;
}
else {
NSLog(#"No taps saved yet");
return 0;
}
}
Try to modify your timer initialization in such way:
myTime = 60;
[countdownTimer invalidate];
countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self
selector:#selector(countDown) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:countdownTimer forMode:NSRunLoopCommonModes];
Check your -countDown method:
myTime = myTime;
Is it typo?

How to hide Text from UILabel in IOS

I'm trying to display a set of numbers on a UILabel sequentially.
For example: I have an array with 5 numbers and each time I want one number from the array to be displayed for 2 seconds on the screen then disappear and then the next number…
This is what i have done so far, but it is not quite what I want. The code below just displays the last number in the array for 2 seconds and then disappears.
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray* myArray = [NSMutableArray array];
for (int i=0; i<5; i++) {
NSNumber *temp;
temp = [myArray objectAtIndex:i];
[self displayNumber:temp];
[myLabel setHidden:NO];
}
}
-(void) displayNumber: (NSNumber *) myNumber{
myLabel.text = [NSString stringWithFormat:#"%#", myNumber];
NSTimer *myTimer;
myTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(hideLabel) userInfo:Nil repeats:NO];
}
-(void) hideLabel{
[myLabel setHidden:YES];
}
I thought about having 5 functions and each gets called sequentially after 2 seconds, but that is very inefficient.
Are there any other ways I can do this?
Change this method
Hope it will works for you what you want
1) Define
NSMutableArray* myArray , int repeatCount; and NSTimer *myTimer in .h file and set its property;
2)
-(void)viewDidLoad
{
[super viewDidLoad];
//This will initialize your timer
myTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(showLabel) userInfo:Nil repeats:NO];
//This will trigger your timer
[myTimer fire];
//Your timer will run in a loop
[[NSRunLoop currentRunLoop] addTimer:myTimer forMode:NSDefaultRunLoopMode];
myArray = [NSMutableArray array];
}
-(void) displayNumber: (NSNumber *) myNumber{
myLabel.text = [NSString stringWithFormat:#"%#", myNumber];
}
-(void) showLabel{
if(repeatCount >= [myArray count])
{
repeatCount = 0;
}
NSNumber *temp = [myArray objectAtIndex:repeatCount];
repeatCount ++;
[self displayNumber:temp];
[myLabel setHidden:NO];
}
I think, that you can use timer with counter.
Create 3 properties:
#property (nonatomic, assign) int counter;
#property (nonatomic, strong) NSArray *array;
#property (nonatomic, strong) NSTimer *timer;
Set them in viewDidLoad: and start timer(with repetition):
- (void)viewDidLoad {
[super viewDidLoad];
_counter = 0;
_array = #[#0, #1, #2, #3, #4, #5];
_timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(nextLabel) userInfo:Nil repeats:YES];
}
Update label:
- (void)nextLabel{
if (_counter == [_array count]) {
//stop in the end of array
[_timer invalidate];
return;
}
[_label setText:[NSString stringWithFormat:#"%#", _array[_counter]]];
_counter++;
}
Pass the array with NSNumber instances to the below method
- (void)updateLabelWithArray:(NSMutableArray *)array
{
if ([array count] != 0)
{
myLabel.text = [NSString stringWithFormat:#"%#", [array firstObject]];
[array removeObjectAtIndex:0];
[self performSelector:#selector(updateLabelWithArray:)
withObject:array
afterDelay:2.0f];
}
}
This should do.
Try this:
// Initialize the timer when you want to change the first number
[NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:#selector(changeNumber:) userInfo:#{#"currentNumber" : [NSNumber numberWithInt:0]} repeats:NO];
-(void)changeNumber:(NSTimer *)timer
{
NSDictionary * dictionary = [timer userInfo];
int number = [(NSNumber *)[dictionary objectForKey:#"currentNumber"] integerValue];
if (number < [numbersArray count])
{
myLabel.text = [NSString stringWithFormat:#"%#", [numbersArray objectAtIndex:number]];
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(changeNumber:) userInfo:#{#"currentNumber" : [NSNumber numberWithInt:number + 1]} repeats:NO];
}
}
try this hope this helps u .. :)
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[super viewDidLoad];
myArray = [[NSMutableArray alloc]initWithObjects:[NSNumber numberWithInt:3],[NSNumber numberWithInt:5],[NSNumber numberWithInt:6],[NSNumber numberWithInt:7],[NSNumber numberWithInt:10], nil]; //your array contains any number you want
index = 0; //showing the numbers from 0th index to last indx
[self.myLabel setHidden:NO];
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(DisplayFortwoSec) userInfo:nil repeats:YES];//call the method reguraly for 2 sec
}
- (void)DisplayFortwoSec
{
[self.myLabel setHidden:YES];
if(index == ([myArray count] - 1))
index = 0;//other stuffs to come out of loop like invalidating the timer
[self.myLabel setHidden:NO];
NSNumber *num = [myArray objectAtIndex:index];
NSString *strNum = [num stringValue];
self.myLabel.text = strNum;
[self.myLabel setHidden:NO];
index ++;
}

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");
}

How to automatic call function every x time [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to call function in every “X” minute?
How to periodically call a certain function?
- (void)viewDidLoad
{
[super viewDidLoad];
[self checkAlert];
}
-(void)checkAlert{
// Server output Alert Note
NSString *alert_note_hostStr = #"http://www.loxleyintranet.com/MobileApplication/xml/alert_note.php";
NSData *alert_note_dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:alert_note_hostStr]];
NSString *alert_note_serverOutput = [[NSString alloc] initWithData:alert_note_dataURL encoding:NSASCIIStringEncoding];
if ([alert_note_serverOutput isEqualToString:#"0"]) {
alertImage.hidden = YES;
alertLabel.hidden = YES;
underMainBG.hidden = YES;
alertLabel.text = [NSString stringWithFormat:#"No Alert"];
}else{
alertLabel.text = [NSString stringWithFormat:#"You've Got Note (%#)" ,alert_note_serverOutput];
}
}
How can I call [self checkAlert]; every x minutes or seconds?
Use [NSTimer scheduledTimerWithTimeInterval:60.0 target:self
selector:#selector(checkAlert) userInfo:nil repeats:YES];.
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(myTimerTick:) userInfo:nil repeats:YES]; // the interval is in seconds...
...then your myTimerTick: method should look like this..
-(void)myTimerTick:(NSTimer *)timer
{
if(some_contiditon)
{
// Do stuff...
}
else
{
[timer invalidate]; //to stop and invalidate the timer.
}
}

Resources