Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7922565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:00:23+00:00 2026-06-03T17:00:23+00:00

I am trying to program an app for the Iphone using Xcode 4.3.1, and

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T17:00:26+00:00Added an answer on June 3, 2026 at 5:00 pm

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying program something very similar to iPhone's Clock application. In Clock.app, when you
I'm running Xcode, trying to get my app to run on my iphone. I'm
I have a universal app for ipad and iphone. What I'm trying to determine
I'm trying to program ARM using Eclipse + CDT + yagarto (gnu toolchain) +
I am trying a program with Swing. I am using a socket to connect
I have a .net MDI application written in vb.net. I'm trying to program a
I have an iPhone app accessing an ASP.NET Webservice for data. Since I'm building
I am trying to program a simple Android app that will stream an internet
I trying to write a program that navigates the local file system using a
I have been trying to set up a program that sends a push notification

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.