I have implemented some code that allows a user to set a time limit to countdown from using UIDatePicker, the users then presses a "Start" button and the countdown is printed in to a UILabel.
I am trying to find a way to stop the timer. Here is the code I have so far that starts the timer:
#implementation P11DetailController
int afterRemainder;
int iRemainder;
NSTimeInterval countDownInterval;
- (void)updateCountDown{
afterRemainder --;
int hours = (int)(afterRemainder)/(60*60);
int mins = (int)(((int)afterRemainder/60) - (hours * 60));
int secs = (int)(((int)afterRemainder - (60 * mins) - ( 60*hours*60)));
NSString *displayText = [[NSString alloc] initWithFormat:#"%02u : %02u :
%02u", hours, mins, secs];
self.displayLabel.text = displayText;
}
then when the user user presses "start":
- (IBAction)startButton:(id)sender {
countDownInterval = (NSTimeInterval)_countdownTimer.countDownDuration;
iRemainder = countDownInterval;
afterRemainder = countDownInterval - iRemainder%60;
[NSTimer scheduledTimerWithTimeInterval:1 target:self
selector:#selector(updateCountDown) userInfo:nil repeats:YES];
}
and finally, when the users presses "Stop":
- (IBAction)stopButton:(id)sender {
//not sure what to add here
}
any ideas?
You need to keep a reference to NSTimer as an ivar:
#implementation P11DetailController
{
NSTimer *myTimer;
}
then:
myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self
selector:#selector(updateCountDown) userInfo:nil repeats:YES];
Then a simple call to:
[myTimer invalidate];
Will stop the timer.
This is all in the documentation which you should consult first.
Related
Got an issue with my countdown timer who don't want to countdown from 60 and reset when 0 is reached. At the moment it just sets its label 0 and start counting down to -1, 2-... How do I get it to start from 60 on iOS in xcode?
.m file
#import "ViewController.h"
#interface ViewController ()
{
int timeTick;
NSTimer *timer;
}
#end
#implementation ViewController
- (IBAction)stopStartBtn:(id)sender {
[timer invalidate];
timeTick = 3;
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(myTicker) userInfo:nil repeats:YES];
}
-(void)myTicker{
timeTick--;
NSString *timeString =[[NSString alloc] initWithFormat:#"%d", timeTick];
self.display.text = timeString;
if(timer >= 0){
[timer invalidate];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
timeTick = 3;
}
#end
.h File
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *display;
- (IBAction)stopStartBtn:(id)sender;
#end
Your code currently starts the timer at 0, decrements it, and then checks it it has reached 60. Obviously that won't happen.
If you want to start at 60 and stop at 0 then you need to set timeTick to 60 in viewDidLoad and check for a value of 0 in myTicker.
And don't forget to call [super viewDidLoad]; in your viewDidLoad method.
You also need to fix your check to see if you've reached zero. Right now you are looking at the timer pointer instead of timeTick integer.
Change:
if(timer >= 0){
[timer invalidate];
}
to:
if (timeTick <= 0) {
[timer invalidate];
}
You are also never setting your timer instance variable. You actually set a local variable of the same name.
Change:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(myTicker) userInfo:nil repeats:YES];
to:
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(myTicker) userInfo:nil repeats:YES];
#interface Ovning1 ()
{
int timeTick;
NSTimer *timer;
}
#end
#implementation Ovning1
- (IBAction)stopStartBtn:(id)sender {
[timer invalidate];
timeTick = 60;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(myTicker) userInfo:nil repeats:YES];
}
-(void)myTicker{
timeTick--;
NSString *timeString =[[NSString alloc] initWithFormat:#"%d", timeTick];
self.display.text = timeString;
if(timeTick <= 0){
[timer invalidate];
}
}
I have a button that when pressed generates random numbers triggered by NSTimer at a certain frequency, and when released I would like it to keep generating them, just more slower and slower until it stops.
- (void)buttonPressed {
_timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:#selector(generateNumber) userInfo:nil repeats:YES];
}
- (void)buttonReleased {
if ([_timer isValid]) {
[_timer invalidate];
}
_timer = nil;
for (float i=0.20; i<1; i += 0.1) {
_timer = [NSTimer scheduledTimerWithTimeInterval:i target:self selector:#selector(generateNumber) userInfo:nil repeats:NO];
}
}
It works fine while the button is pressed, but once I release it actually accelerate and then stops all in a sudden. Any idea why it wouldn't be working or any alternative way I can reach the wanted result?
Once your button is released, generation accelerates because at that moment you schedule 8 timers with intervals 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8 and 0.9 starting at the time of button release, which means each will execute 0.1s after the other, which is a smaller interval than your original timer (0.2s).
This is an alternative
#interface ViewController ()
#property (nonatomic, retain) NSTimer *timer;
#property (nonatomic) float curInterval;
#end
#implementation ViewController
- (IBAction)buttonDown {
self.curInterval = 0.1;
[self updatePressed];
}
- (void)updatePressed {
[self generateNumber];
self.timer = [NSTimer scheduledTimerWithTimeInterval:self.curInterval target:self selector:#selector(updatePressed) userInfo:nil repeats:NO];
}
- (IBAction)buttonUp {
[self.timer invalidate];
[self updateUp];
}
- (void)updateUp {
[self generateNumber];
self.curInterval += 0.1;
if (self.curInterval < 1.0f) {
self.timer = [NSTimer scheduledTimerWithTimeInterval:self.curInterval target:self selector:#selector(updateUp) userInfo:nil repeats:NO];
}
}
#end
In Button release method you are using for loop which keeps on executing untill it reaches interval of 1. What you need to do is take a global float value and initialize it to 0.20 at start. On button pressed pass that value. On button release increment the global variable and pass that value to NSTimer instead of for loop
float numTimer = 0.20f;
- (void)buttonPressed {
_timer = [NSTimer scheduledTimerWithTimeInterval:numTimer target:self selector:#selector(generateNumber) userInfo:nil repeats:YES];
}
- (void)buttonReleased {
if ([_timer isValid]) {
[_timer invalidate];
}
_timer = nil;
numTimer = numTimer + 0.1f;
_timer = [NSTimer scheduledTimerWithTimeInterval:numTimer target:self selector:#selector(reGenerateNumber) userInfo:nil repeats:NO]; }
}
-(void) reGenerateNumber {
[self generateNumber];
if ([_timer isValid]) {
[_timer invalidate];
}
_timer = nil;
numTimer = numTimer + 0.1f;
_timer = [NSTimer scheduledTimerWithTimeInterval:numTimer target:self selector:#selector(reGenerateNumber) userInfo:nil repeats:NO];
}
After the end of our game the NSTimer does not stop.
-(void)StartGame{
Countdown = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(TimeDecrease) userInfo:nil repeats:YES];
}
-(void)TimeDecrease{
StartTime = StartTime - 1;
Timer.text = [NSString stringWithFormat:#"%d",StartTime];
if(StartTime == 0){
[self EndGame];
}
}
We tried [Countdown release] and disabled ARC but it throws an error.
We are trying to accomplish a timer that resets to 15 at the beginning of every new game.
Try:
[Countdown invalidate];
Countdown = nil;
So, Here is the problem. I am trying to make a timer for my game however it doesnt seem to run.
I started by making a property for nstimer:
#property (nonatomic) NSTimer gameTimer;
and synthesizing it:
#synthesize gameTimer = _gameTimer;
then I use this method to set it:
-(NSTimer *) gameTimer{
if (_gameTimer == nil) _gameTimer = [[NSTimer alloc]init];
return _gameTimer;
}
however when i try to start the timer through a uibutton:
- (IBAction)play:(UIButton *)sender {
_levelNumber = 1;
_en1v = -1;
_en2v = 1;
_en3v = -1;
[self setPath];
_gameTimer = [NSTimer timerWithTimeInterval:1.0 target:self selector:#selector(onTimer:) userInfo:nil repeats:YES];
self.play.hidden = TRUE;
}
it doesn't work.
I put an nslog into the onTimer: method and found out that the timer just isn't firing some how?
Am I making an obvious mistake?
You have not started the timer at all.
Use:
_gameTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(onTimer:) userInfo:nil repeats:YES];
And while stopping, (may be in stop: method)
[_gameTimer invalidate];
_gameTimer = nil;
I made a stopwatch application recently and it had a few glitches.
If I hit the stop button twice in a row, the entire app would crash.
If I hit the start button twice in a row, the timer would run twice as fast and the stop button would stop working.
How do I fix this problem?
Here is the code in my .h file:
IBOutlet UILabel *time;
IBOutlet UILabel *time1;
IBOutlet UILabel *time2;
NSTimer *myTicker;
NSTimer *myTicker2;
NSTimer *myTicker3;
}
- (IBAction)start;
- (IBAction)stop;
- (IBAction)reset;
- (void)showActivity;
- (void)showActivity1;
- (void)showActivity2;
#end
and here is my code in the .m file:
- (IBAction)start {
myTicker = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(showActivity) userInfo:nil repeats:YES];
myTicker2 = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:#selector(showActivity1) userInfo:nil repeats:YES];
myTicker3 = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:#selector(showActivity2) userInfo:nil repeats:YES];
}
- (IBAction)stop {
[myTicker invalidate];
[myTicker2 invalidate];
[myTicker3 invalidate];
}
- (IBAction)reset {
time.text = #"00";
time1.text = #"00";
time2.text = #"00";
}
- (void)showActivity {
int currentTime = [time.text intValue];
int newTime = currentTime + 1;
if (newTime == 60) {
newTime = 0;
}
time.text = [NSString stringWithFormat:#"%d", newTime];
}
- (void)showActivity1 {
int currentTime1 = [time1.text intValue];
int newTime1 = currentTime1 + 1;
if (newTime1 == 10) {
newTime1 = 0;
}
time1.text = [NSString stringWithFormat:#"%d", newTime1];
}
- (void)showActivity2 {
int currentTime2 = [time2.text intValue];
int newTime2 = currentTime2 + 1;
time2.text = [NSString stringWithFormat:#"%d", newTime2];
}
Set the stop button's userInterActionEnabled property to NO and the start button's to YES when the -stop method is fired. Then switch and set the stop button's userInterActionEnabled to YES and the start button's to NO when -start is fired.
You should create a private BOOL variable "isRunning", which is checked when clicked on Stop or Start like:
- (IBAction)stop {
if(!isRunning) return;
[myTicker invalidate];
[myTicker2 invalidate];
[myTicker3 invalidate];
self.isRunning = NO;
}
etc. Also ignoring user interactions is general a good idea (like CodaFi suggested), but only fights the symptoms ;-) You really should do both checks.