Re: GUI Time Field
Re: GUI Time Field
- Subject: Re: GUI Time Field
- From: Justin Giboney <email@hidden>
- Date: Tue, 22 Apr 2008 16:24:04 -0600
Thank you for response, I hope I have improved my code.
I am binding my text field to the variable "theDateTime." I know this
works because if I uncomment the commented line, I can see the date
and time.
The problem is that the UI isn't updating.
Thanks again.
Justin Giboney
#import "Controller.h"
@implementation Controller
- (id) init {
[super init];
//[self setDateTime: [NSDate date]];
[NSTimer scheduledTimerWithTimeInterval: 1 target: self selector:
@selector(runClock:) userInfo: nil repeats: YES];
return self;
}
- (void) setDateTime: (NSDate *) theDate {
theDateTime = theDate;
NSLog(@"setting the date %@", [theDateTime description]);
}
- (void) runClock: (NSTimer *) theTimer {
[self setDateTime: [NSDate date]];
}
@end
On Apr 22, 2008, at 2:41 PM, Shawn Erickson wrote:
On Tue, Apr 22, 2008 at 12:34 PM, Justin Giboney
<email@hidden> wrote:
I am trying to put a automatic date and time field into my GUI.
I have a text field connected to a variable called "theDateTime" in
my
controller class. If I set the variable manually the text field
works just
fine. But I want it to update automatically. My code is attached.
Thank you
Justin Giboney
#import "Controller.h"
@implementation Controller
- (id) init {
[super init];
[NSThread detachNewThreadSelector:@selector(runClock)
toTarget: self
withObject: nil];
return self;
}
- (void) setDateTime {
NSAutoreleasePool *tempPool = [[NSAutoreleasePool alloc]
init];
theDateTime = [NSDate date];
[tempPool release];
}
Sorry hit send by mistake on my prior email...
In the above you don't retain the object you assign to "theDateTime"
so it goes away when the current thread pool is drained. Also it isn't
clear how you expect the above simple setting of an ivar to trigger
your UI to update (you have it bound?). If so you shouldn't update UI
in this way from a secondary thread (especially if bindings are used).
You should use a proper setter (e.g. -setDateTime:(NSDate*)date) that
does the proper memory management and have your thread function create
the date object and call the setter. If you want to use a secondary
thread use performSelectorOnMainThread to get the update to take place
from the context of the main thread.
-Shawn
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden