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

  • Home
  • SEARCH
  • 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 4125354
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:51:30+00:00 2026-05-20T23:51:30+00:00

I’ve been trying to figure out how to setup an NSTimer to allow me

  • 0

I’ve been trying to figure out how to setup an NSTimer to allow me to print the current time in a UILabel within a View, and have it update every second (no finer resolution required – just a simple clock).

At first, I wasn’t using a NSRunLoop, but if I try and include one, the execution just “spins” inside the loop, blocking further execution. I have posted my code below.

-(id) printCurrentTime {

now = [NSDate date];
dateFormat = [[NSDateFormatter alloc] init];

[dateFormat setTimeStyle:NSDateFormatterMediumStyle];

NSString *nowstr = [dateFormat stringFromDate:now];
[dateFormat release];
NSLog(@"Current time is: %@",nowstr);

return nowstr;
}

And in the ViewController source file, I execute as per:

TimeStuff *T = [[TimeStuff alloc] init];
NSString *thetime = [T printCurrentTime];
[timelabel setText:thetime];
[T release];
[self.view addSubview:timelabel];


NSTimer *timmeh = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(printCurrentTime) userInfo:nil repeats:YES];

[[[NSRunLoop currentRunLoop] addTimer:timmeh forMode:NSDefaultRunLoopMode] run];

The “TimeStuff” class is effectively an empty class, save for the printCurrentTime function.

Questions:

1) Should I be including the RunLoop in the AppDelegate class? I am having trouble visualising how this all should hang together, as in – what are the steps to achieving a Loop based on Timer to update a text label with the up-to-second time. Pretty stumped.

2) In the event that I should use a NSThread, should that also be in it’s own class / the Delegate class.

3) Is the ViewController class totally out of bounds for looping/timers, and simply the “eye candy” class, with callbacks to functions in the Delegate class?

Thank you for your time and patience.

  • 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-05-20T23:51:31+00:00Added an answer on May 20, 2026 at 11:51 pm

    You don’t need to deal with run loops at all.

    This line :

    NSTimer *timmeh = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(printCurrentTime) userInfo:nil repeats:YES];
    

    will create a timer and attach it to the current thread’s run loop for you. You don’t need the [NSRunLoop addTimer:forMode:] call at all – you can delete that line.

    PS You certainly don’t need to go as far as NSThreads!


    EDIT Regarding your comment :

    You will need to make an instance of your TimeStuff class for the timer to use if that’s where your printCurrentTime method is. i.e.

    @interface MyViewController : UIViewcontroller {
        TimeStuff *timeStuff
    }
    

    and in your viewDidLoad method:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        ...
    
        // Create our timestuff if we don't have one already
        if (nil == timeStuff)
            timeStuff = [[TimeStuff alloc] init];
    
        // Start the timer
        [NSTimer scheduledTimerWithTimeInterval:1.0 target:timeStuff selector:@selector(printCurrentTime) userInfo:nil repeats:YES];
    

    and not forgetting dealloc

    - (void)dealloc {
        [timeStuff release];
        ...
        [super dealloc];
    }
    

    Passing in the timeStuff as the target for the timer tells it where to look for the printCurrentTime method!

    Hope that helps,

    PS All the line @class TimeStuff does is tell the compiler that there is a class called TimeStuff. It has no idea that you want to use it for your timer’s selector!

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I am trying to loop through a bunch of documents I have to put
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is

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.