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.
timerActiveis redundant because you can just check ifself->timeris non-nil.secondsandminutescould 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 useIBOutletfor 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
ViewControllerobject 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.ViewControllerwould 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?