Creating multiple timers that are self invalidating - objective-c

I am trying to program an app for the Iphone using Xcode 4.3.1, and what I would like to be able to do is press a button and touch somewhere on the touchscreen and have a label(with a timer attached) show up and count down from some number. When the timer reaches 0:00, I want it to invalidate itself and to do this I need to keep it's reference.
I do not know how many total number of labels/timers I will use before-hand so I was thinking I would use an array to store each one. I have very little knowledge knowledge of the Objective-C language. Everything I have done so far has just been replicating examples I have seen in other stackoverflow questions, trying to understand them. I have been able to build a relatively functional timer so far.
Below is my current code for my timer. Currently it is just a pre-made button connected to a pre-made label with all the functionality that I want my timer to have. It starts at 5 minutes, formats itself to minutes:seconds, and invalidates the timer when it reaches 0:00. The button also acts as a stop/reset function once the timer has started.
ViewController.h
#interface ViewController : UIViewController{
IBOutlet UILabel *timerDisplay;
NSTimer *timer;
bool timerActive;
int MainInt;
int minutes;
int seconds;
}
#property (nonatomic, retain) UILabel *timerDisplay;
-(IBAction)start:(id)sender;
-(void)countdown;
-(void)timerStop;
-(void)timeFormat;
#end
ViewController.m
#implementation ViewController
#synthesize timerDisplay;
-(void)timeFormat{
seconds = MainInt % 60;
minutes = (MainInt - seconds) / 60;
timerDisplay.text = [NSString stringWithFormat:#"%d:%.2d", minutes, seconds];
}
-(void)countdown {
MainInt -= 1;
[self timeFormat];
if (MainInt <=0){
timerActive = NO;
[self->timer invalidate];
self->timer = nil;
}
}
-(IBAction)start:(id)sender {
MainInt = 300;
[self timeFormat];
if(timerActive == NO){
timerActive = YES;
self->timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(countdown) userInfo:nil repeats:YES];
}
else {
timerActive = NO;
[self->timer invalidate];
self->timer = nil;
}
}
Any help at all would be appreciated. I would mostly like help with being able to have multiple timers at one time using an array or something. Hardcoding a large amount of timers would be a silly thing to do.
Cheers.
EDIT 2:
I have the code below to act as my App, this is pretty much exactly what I want it to do. Except in this code, I can only have one timer running at a time, this is my problem.
In this code I store the value of the most recent tap on the touchscreen(to use its co-ordinates to place the timer somewhere on the screen), and a single button that when I press it, a running timer(with UILabel) will appear at the location of the previously stored tap. It will run until completed and then invalidate itself and remove itself from the view.
This works fine.
However when I press the button again before the original timer has completed, it will simply create a new timer(with UILabel) at the new location. This new timer will work fine, but the old one has lost its timer reference so it cannot finish, and I cannot remove it.
ViewController.h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController{
UILabel *timerDisplay;
NSTimer *timer;
CGPoint startPoint;
int MainInt;
}
#property CGPoint startPoint;
-(IBAction)startTimer:(id)sender;
-(void)countdown;
-(void)timeFormat;
-(void)start;
#end
ViewController.m:
#implementation ViewController
#synthesize startPoint;
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *theTouch = [touches anyObject];
startPoint = [theTouch locationInView:self.view];
}
-(IBAction)startTimer:(id)sender {
timerDisplay = [[UILabel alloc] initWithFrame:CGRectMake(startPoint.x -15, startPoint.y - 15, 50, 50)];
timerDisplay.textAlignment = UITextAlignmentCenter;
timerDisplay.text = [NSString stringWithFormat:#"%i", MainInt];
timerDisplay.backgroundColor = [UIColor greenColor];
timerDisplay.textColor = [UIColor whiteColor];
[self.view addSubview:timerDisplay];
[self start];
}
-(void)timeFormat{
int seconds = MainInt % 60;
int minutes = (MainInt - seconds) / 60;
timerDisplay.text = [NSString stringWithFormat:#"%d:%.2d", minutes, seconds];
}
-(void)countdown {
MainInt -= 1;
[self timeFormat];
if (MainInt <= 0){
[self->timer invalidate];
self->timer = nil;
[timerDisplay removeFromSuperview];
}
}
-(void)start {
MainInt = 5;
[self timeFormat];
if(self->timer == nil){
self->timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(countdown) userInfo:nil repeats:YES];
}
}
#end
I was hoping someone could help me in creating a new class to hold each of the new timer instances and keep the references to them. I lack the knowledge of Objective C to get this done. Or perhaps you could link me to an example that could help me.
Cheers.

Your code looks OK to me. I would say that you have redundant instance variables. timerActive is redundant because you can just check if self->timer is non-nil. seconds and minutes could just be local to -timeFormat, they don't need to be instance variables.
Is the issue that you don't know how to generalize this to multiple buttons-label pairs with multiple timers? I'd suggest that you create a new class to act as a controller for each button-label pair. The class would derive from NSObject. What are currently instance variables of your view controller class would instead be instance variables of this new class, although you wouldn't necessarily use IBOutlet for the label. (An outlet is connected in a NIB, but these would presumably be created dynamically and would be connected in code.)
You'd allocate and initialize an instance of this new class for each button-label pair you want. You'd pass in the pointer to the label and the starting time value. You might also pass in the pointer to the ViewController object which owns it, so that it can inform it when it has stopped or something like that. The corresponding button would be set to target this new controller. ViewController would keep track of each of these controllers in a mutable array. When it is done with one, it would remove its label, its button, and the controller.
Does that help?

Related

Where do I put my loop?

I really need some help on where to put this loop:
int timer = 10;
while (timer >= 0) {
[secondsLeft setText:[NSString stringWithFormat:#"%d", timer]];
NSLog(#"%d", timer);
timer--;
sleep(1);
}
Anyways, wherever I put this loop I get some sort of error except for under the IBAction where it works perfectly except it delays the button press by 10 seconds :P
Here's my .m file
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize answer;
#synthesize secondsLeft;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
}
- (IBAction)answerButton:(id)sender {
NSString *str = answer.text; // Takes user input from answer.text
int answerOne = [str intValue]; // converts answer into an integer
if(answerOne == 30) {
self.secondsLeftToAnswer.text = #"Correct!";
} else {
self.secondsLeftToAnswer.text = #"You Suck at Math!";
}
}
#end
Anyways can someone please tell me how I can implement this loop into my code so that the loop displays its output to a UILabel (secondsLeft is the UILabel, loop is supposed to display a countdown from 10 to 0 in the UILabel) ?
EDIT: How would I go about implementing a NSTimer to do what I want (countdown from 10)? I tried to set one up but they are so confusing. Thanks for the help so far Jasarien!
The answer is "nowhere". Cocoa is an event-driven system and such loops stop the main thread from processing events and you won't see the text of the label change and user-interaction will be disabled. See the Main Event Loop documentation for details.
Instead use an NSTimer (for example), which works with the runloop to allow periodic method invocation.
In order to show countdown you need to update the label after specific time.
Below is the code depicting the functionality you want.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
count = 10;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target: self
selector: #selector(handleTimer)
userInfo: nil
repeats: YES];
}
-(void)handleTimer{
if (count >= 0) {
[lblTimer setText:[NSString stringWithFormat:#"%ld",(long)count]];
count--;
}else{
[timer invalidate];
}
}
Hope this will help you. Happy coding :)

How to update UILabel

I have a UILabel that I would like to update. It has been added to the class by ctrl-cicking and adding through the XIB file. Im trying to update the label text after waiting for a short delay. As of now, there is nothing else going on except for the code below. Howver when I run this, the simulator blanks out for a moment and takes me straight to the last updated text. It doesnt show me the 100 just the 200.
How do I get the label to update like I want it to. Ultimately Im trying to have a timer of sorts decrement inside the label.
Label linked from the XIB to header file:
#property (strong, nonatomic) IBOutlet UILabel *timeRemainingLabel;
In Implmentation:
- (void)viewDidLoad
{
[super viewDidLoad];
self.timeRemainingLabel.text = #"100";
sleep(1);
self.timeRemainingLabel.text = #"200";
}
It has been synthesized.
XCode 4.3.2, Mac OSX 10.7.3, iOS Simulator 5.1 (running iPad), iOS 5
The problem with your implementation is that the execution sequence does not leave the method while in the sleep. This is the problem, because the UI subsystem never gets a chance to update the label to the "100" value before it gets a command to set it to "200".
To do this correctly, first you need to create a timer in your init method, like this:
timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:#selector(updateLabel) userInfo:nil repeats: YES];
Then you need to write the code for your updateLabel method:
-(void) updateLabel {
NSInteger next = [timeRemainingLabel.text integerValue]-1;
timeRemainingLabel.text = [NSString stringWithFormat:#"%d", next];
}
It will never show you 100 like this because you are using sleep here which is stopping the execution of your program and just after 1 sec of sleep you are updating the text. If you want to do this then you can use an NSTimer for this.
Change your above code like this:
- (void)viewDidLoad
{
[super viewDidLoad];
self.timeRemainingLabel.text = #"100";
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(updateLabel) userInfo:nil repeats:NO];
}
- (void) updateLabel
{
self.timeRemainingLabel.text = #"200";
}
Your view doesn't appear till the view hasn't loaded and the text of the label timeRemainingLabel is #"200" when that happens. So you do not see the text changing. Use an NSTimer to do this instead and assign the text to the label in the selector:
timer = [NSTimer scheduledTimerWithTimeInterval:timeInSeconds target:self selector:#selector(updateText) userInfo:nil repeats: YES/NO];
and in your update method, set the latest text as per your requirement:
-(void) updateText {
self.timeRemainingLabel.text = latestTextForLabel;
}

NSTimer within a class

I have a timer class set up that is basically handling all of count down timer logic. All it does is on button press - counts from 60 to 0 seconds.
I have the following code in a Timer.m class.
- (void)advanceTimer
{
self.lengthOfTime = [NSNumber numberWithInt:self.lengthOfTime.intValue - 1];
NSLog(#"%#",self.lengthOfTime);
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(advanceTimer) userInfo:nil repeats:NO];
}
- (void)startCountdown
{
if (!self.lengthOfTime) self.lengthOfTime = [NSNumber numberWithInt:60];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(advanceTimer) userInfo:nil repeats:NO];
}
What I am looking to do is create a timer object in my View Controller that will update a label from the ViewController.m. Currently - the class works because I can NSLog from the Timer class and it counts down correctly. I thought about having the advanceTimer method return - but I can't seem to wrap my head around how to update the label in the ViewController with the returned data.
The only way I cold get the return to work was to have a button that refreshed the label to the correct countdown time... I can't get it to automatically count down...
Well, if you know how to update a label by clicking a button, you have everything in place to connect everything else:
If your view controller has an IBOutlet for the label and an IBAction that updates it, why not call the view-controller's action in your advanceTimer method?
Yet easier, you could connect your timer class to the label.
You might do it like this:
// Timer.h:
#interface Timer : NSObject
#property (retain, nonatomic) IBOutlet UILabel *timeLabel;
#property (assign, nonatomic) NSInteger secondsRemaining;
#property (assign, nonatomic) NSTimer *timer;
- (IBAction)startCountdown:(id)sender;
- (IBAction)stopCountdown:(id)sender;
- (void)timerFired:(NSTimer *)timer;
#end
// Timer.m
#implementation Timer
#synthesize timeLabel = timeLabel_;
#synthesize secondsRemaining = secondsRemaining;
#synthesize timer = timer_;
- (void)setTimer:(NSTimer *)timer
{
if (timer = timer_)
return;
[timer_ invalidate];
timer_ = timer;
}
- (void)scheduleTimer
{
if (self.secondsRemaining <= 0) {
self.timer = nil;
} else {
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(timerFired:) userInfo:nil repeats:NO];
}
}
- (void)timerFired:(NSTimer *)timer
{
self.secondsRemaining -= 1;
NSString *displayString = [NSString stringWithFormat:#"%d", self.secondsRemaining];
self.timeLabel.text = displayString;
[self scheduleTimer];
}
- (IBAction)startCountdown:(id)sender
{
self.secondsRemaining = 60;
[self scheduleTimer];
}
- (IBAction)stopCountdown:(id)sender
{
self.timer = nil;
}
- (void)dealloc
{
[timeLabel_ release];
[super dealloc];
}
#end
This code has a two benefits:
You can cancel your timer.
Your view controller does not need to know anything about this — you can set this up in interface builder, entirely.
Correct me if I'm wrong, but I think you should retain that NSTimer in a class member. Otherwise the timer is destroyed when finishing the method.
#property (nonatomic,retain) NSTimer * yourTimer;
In the .m file
#synthesize yourTimer;
And then
- (void)advanceTimer
{
self.lengthOfTime = [NSNumber numberWithInt:self.lengthOfTime.intValue - 1];
NSLog(#"%#",self.lengthOfTime);
self.yourTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(advanceTimer) userInfo:nil repeats:NO];
}
- (void)startCountdown
{
if (!self.lengthOfTime) self.lengthOfTime = [NSNumber numberWithInt:60];
self.yourTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(advanceTimer) userInfo:nil repeats:NO];
}
I fixed also your NSNumber alloc, so there are no memory leaks ;)
Explaining it, NSTimer scheduledTimerWithTimeInterval: gives you an NSTimer with autorelease. If this Timer is not retained by some member, it gets released as soon as the method ends and, since no other pointer is retaining it, it gets freed. Maybe that's the explanation ;). Never worked with NSTimers
The countdown class needs to save a link back to the ViewController and then call a method on it.
One approach would be to use the delegate pattern. Have the Countdown class's init method as initWithDelegate:(id)delegate and a predefined callback method (like updateCountdown:(NSNumber*)currentCountdown). The ViewController sends itself as the delegate and implements the update method.
Another approach is the target/action pattern. NSTimer uses this approach. The init method would be initWithTarget:(id)target selector:(SEL)selector. The ViewController sends itself as the target and whatever selector it wants to use (as long as it takes an NSNumber as it's sole argument).
In both cases in advanceTimer the Countdown class will use performSelector:withObject: to call the ViewController's update method.
If you really want a true one second timer then set it to repeat. Otherwise you will drift slowly by the amount of time the advanceTimer method takes to fire and complete. At the end of the countdown use a reference to the timer to invalidate it.

Objective C & iOS: running a timer? NSTimer/Threads/NSDate/etc

I am working on my first iOS app, and have run in the first snag I have not been able to find a good answer for.
The problem: I have a custom UIGestureRecognizer and have it all wired up correctly, and I can run code for each touch in the #selector after recognition. This has been fine for most things, but it's a little too much input for others.
My goal: To make a timer that triggers at a specified interval to run the logic, and to be able to cancel this at the moment touches are cancelled.
Why I am asking here: There are a lot of possibilities for solutions, but none has stood out as the best to implement. So far it seems like
performSelector (and some variations on this)
NSThread
NSTimer
NSDate
Operation Queues
I think I found some others as well...
From all the research, some form of making a thread seems the route to go, but I am at a loss at which would work best for this situation.
An example of an implementation: an NSPoint is taken every 0.10 seconds, and the distance between the previous and current point is taken. [Taking the distance between every point was yielding very messy results].
The relevant code:
- (void)viewDidLoad {
CUIVerticalSwipeHold *vSwipe =
[[CUIVerticalSwipeHold alloc]
initWithTarget:self
action:#selector(touchHoldMove:)];
[self.view addGestureRecognizer:vSwipe];
[vSwipe requireGestureRecognizerToFail:doubleTap];
}
...
- (IBAction)touchHoldMove:(UIGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
}
if (sender.state == UIGestureRecognizerStateBegan) {
}
//other stuff to do goes here
}
Use an NSTimer
Set it up like this:
theTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(yourMethodThatYouWantRunEachTimeTheTimerFires) userInfo:nil repeats:YES];
Then when you want to cancel it, do something like this:
if ([theTimer isValid])
{
[theTimer invalidate];
}
Note that in the above example you would need to declare the "theTimer" instance of NSTimer where it will be available to both methods. In the above example the "0.5" means that the timer will fire twice a second. Adjust as needed.
For the sake of completeness, I am adding my final implementation here (not sure this is the way to do it, but here goes)
.h
#interface {
NSTimer *myTimer;
}
#property (nonatomic, retain) NSTimer *myTimer;
.m
#synthesize myTimer;
-------------------------------------------
- (void)viewDidLoad {
//Relevant snipet
CUIVerticalSwipeHold *vSwipe =
[[CUIVerticalSwipeHold alloc]
initWithTarget:self
action:#selector(touchHoldMove:)];
[self.view addGestureRecognizer:vSwipe];
[vSwipe requireGestureRecognizerToFail:doubleTap];
}
-------------------------------------------
- (IBAction)touchHoldMove:(UIGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
//Cancel the timer when the gesture ends
if ([myTimer isValid])
{
[myTimer invalidate];
}
}
}
if (sender.state == UIGestureRecognizerStateBegan) {
//starting the timer when the gesture begins
myTimer = [NSTimer scheduledTimerWithTimeInterval:someTimeIncrement
target:self
selector:#selector(someSelector)
userInfo:nil
repeats:YES];
}
}

UIButton Touch and Hold

I haven't found a very easy way to do this. The ways I've seen require all these timers and stuff. Is there any easy way I can hold a UIButton and cause it to repeat the action over and over until it gets released?
You can do the following: Make an NSTimer that will start up when the app starts or in viewDidLoad and also make a boolean.
For example:
//Declare the timer, boolean and the needed IBActions in interface.
#interface className {
NSTimer * timer;
bool g;
}
-(IBAction)theTouchDown(id)sender;
-(IBAction)theTouchUpInside(id)sender;
-(IBAction)theTouchUpOutside(id)sender;
//Give the timer properties.
#property (nonatomic, retain) NSTimer * timer;
Now in your implementation file (.m):
//Synthesize the timer
#synthesize timer;
//When your view loads initialize the timer and boolean.
-(void)viewDidLoad {
g = false;
timer = [NSTimer scheduledTimerWithInterval: 1.0 target:self selector:#selector(targetMethod:) userInfo:nil repeats: YES];
}
Now make an IBAction for "Touch Down" set the boolean to lets say true. Then make another IBAction button for "Touch Up Inside" and "Touch Up Outside" assign the boolean to false.
For example:
-(IBAction)theTouchDown {
g = true;
}
-(IBAction)theTouchUpInside {
g = false;
}
-(IBAction)theTouchUpOutside {
g = false;
}
Then in that NSTimer method, put the following:(assume g is the boolean you have declared)
-(void) targetmethod:(id)sender {
if (g == true) {
//This is for "Touch and Hold"
}
else {
//This is for the person is off the button.
}
}
I hope this simplifies everything... I know it still uses a timer but there is not another way.
Unfortunately, it still looks like you have to code this functionality for yourself. simplest way (You still need a timer though):
A function that performs the action you want to repeat:
-(void) actionToRepeat:(NSTimer *)timer
{
NSLog(#"Action triggered");
}
in your .h file declare and set a property for a timer:
#interface ClassFoo
{
NSTimer* holdTimer;
}
Then in the .m make two IBActions:
-(IBAction) startAction: (id)sender
{
holdTimer = [NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:#selector(actionToRepeat:) userInfo:nil repeats:YES];
[holdTimer retain];
}
-(IBAction) stopAction: (id)sender
{
[holdTimer invalidate];
[holdTimer release];
holdTimer = nil;
}
Then Just link to the Touch Down event in IB from the button to startAction and the Touch Up Inside to the 'Stop Action'. It isn't a one liner but it allows you to customise the rate the action repeats as well as allowing you to trigger it from another outlet/action.
You might consider subclassing UIButton and adding this functionality if you are going to be using this functionality often - then it is only (slightly) painful to implement the first time.
An other way to use this NBTouchAndHoldButton. This is exactly what you want, and very easy to implement it:
TouchAndHoldButton * pageDownButton = [TouchAndHoldButton buttonWithType:UIButtonTypeCustom];
[pageDownButton addTarget:self action:#selector(pageDownAction:) forTouchAndHoldControlEventWithTimeInterval:0.2];
Good luck!
I cannot reply to the first one, but this line:
timer = [NSTimer scheduledTimerWithInterval: 1.0 target:self selector:#selector(targetMethod:) userInfo:nil repeats: YES];
for at least iOS 4.1 and newer needs to be:
timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:#selector(targetMethod:) userInfo:nil repeats: YES];
I know this is an old question, but as an easy way, like to consider using "[NSObject performSelector:withObject:afterDelay:]" to repeatedly invoke methods in any particular time interval.
In this case:
NSTimeInterval someTimeInterval = 1;
- (IBAction)action:(id)sender {
UIButton * const button = sender;
if (button.state != UIControlStateHighlighted) {
return;
}
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:_cmd object:sender];
[self performSelector:_cmd withObject:sender afterDelay:someTimeInterval];
}