Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-12-2012, 12:36 PM   #1 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 38
iOS.Lover is on a distinguished road
Default Updating NSDateFormatter with NSTimer

Hi, I have created a custom class which show time date formatter and I need a timer like method to update the seconds, so here is my code :

CustomClass.m


Code:
- (NSString *) showLocaleTime {

        [timeFormater setDateFormat:@"HH:mm:ss"];
        NSString *timer = [timeFormater stringFromDate:[NSDate date]];
        timer = [NSString stringWithFormat:@"%@",timer];
        [timeFormater release];


    return timer;
}


- (void) updateLocaleTime {

    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(showLocaleTime) userInfo:nil repeats:YES];

}

viewController.m :

Code:
CustomClass *time = [[CustomClass alloc]init];
label.text = [time showLocaleTime];

[time updateLocaleTime];
\


But the problem is the updateLocaleTime does not call to update seconds ! am I missing something ? Thanks
iOS.Lover is offline   Reply With Quote
Old 01-12-2012, 01:28 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by iOS.Lover View Post
Hi, I have created a custom class which show time date formatter and I need a timer like method to update the seconds, so here is my code :

CustomClass.m


Code:
- (NSString *) showLocaleTime {

        [timeFormater setDateFormat:@"HH:mm:ss"];
        NSString *timer = [timeFormater stringFromDate:[NSDate date]];
        timer = [NSString stringWithFormat:@"%@",timer];
        [timeFormater release];


    return timer;
}


- (void) updateLocaleTime {

    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(showLocaleTime) userInfo:nil repeats:YES];

}

viewController.m :

Code:
CustomClass *time = [[CustomClass alloc]init];
label.text = [time showLocaleTime];

[time updateLocaleTime];
\


But the problem is the updateLocaleTime does not call to update seconds ! am I missing something ? Thanks

You have showLocalTime set up as a method with no parameters, that returns a string as a result.

NSTimer methods need to take a single parameter, the timer itself, and not return any result. To quote the Apple docs from NSTimer:


Quote:
The message to send to target when the timer fires. The selector must have the following signature:
Code:
- (void)timerFireMethod:(NSTimer*)theTimer
You need to write a timer method that calls your showLocaleTime method, takes the result, and does something with it.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 01-12-2012, 03:37 PM   #3 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 38
iOS.Lover is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
You have showLocalTime set up as a method with no parameters, that returns a string as a result.

NSTimer methods need to take a single parameter, the timer itself, and not return any result. To quote the Apple docs from NSTimer:
You need to write a timer method that calls your showLocaleTime method, takes the result, and does something with it.
Sorry my code was wrong , here is showLocaleTime , would you please reconsider it again ?

Code:
- (NSString *) showLocaleTime {

NSDateFormatter *timeFormater = [[NSDateFormatter alloc] init];
timeFormater = [setDateFormat:@"HH:mm:ss "];

NSString *currDay = [timeFormater stringFromDate:[NSDate date]];
currDay = [NSString stringWithFormat:@"%@",currDay];
[timeFormater release];


    return timer;
}


- (void) updateLocaleTime {

    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(showLocaleTime) userInfo:nil repeats:YES];

}
iOS.Lover is offline   Reply With Quote
Old 01-12-2012, 05:15 PM   #4 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

His post still applies. You are calling showLocaleTime with your timer. showLocaleTime returns a NSString. It should returning nothing. In your showLocaleTime method, you should set a UILabel's text property with your currDay NSString variable.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.

New app - See screenshots and details at www.globaclock.com.

If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
Domele is offline   Reply With Quote
Old 01-12-2012, 10:06 PM   #5 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by iOS.Lover View Post
Sorry my code was wrong , here is showLocaleTime , would you please reconsider it again ?

Code:
- (NSString *) showLocaleTime {

NSDateFormatter *timeFormater = [[NSDateFormatter alloc] init];
timeFormater = [setDateFormat:@"HH:mm:ss "];

NSString *currDay = [timeFormater stringFromDate:[NSDate date]];
currDay = [NSString stringWithFormat:@"%@",currDay];
[timeFormater release];


    return timer;
}


- (void) updateLocaleTime {

    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(showLocaleTime) userInfo:nil repeats:YES];

}

A method that's invoked by a timer must have specific signature or it won't get called. Go back and read my previous post.

The method the timer calls can't return a value. It must have a single parameter, which is the timer itself. This is not a "well maybe" situation. Do it that way, or it doesn't work. Period.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Tags
nsdate, nsdateformatter, nstimer

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 374
11 members and 363 guests
7twenty7, Atatator, glenn_sayers, guusleijsten, iphonedevshani, j.b.rajesh@gmail.com, QuantumDoja, sacha1996, Sami Gh, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,674
Threads: 94,122
Posts: 402,907
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Atatator
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:40 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0