Reputation: 163
I have a textfield label in my XIB file which is used to display some text that I want to dynamically load. Here is the current flow of my applications:
-In XIB file, the textfield is set to a static text -Upon running the application, the window is loaded with the textfield and static text -After loading, windowDidLoad is called and which changes the text into something dynamic
- (void)windowDidLoad
{
[super windowDidLoad];
NSDate *currentDate = [NSDate date];
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:currentDate];
[myTextField setStringValue:[NSString stringWithFormat:@"Year: %ld", [components year]]];
}
Unfortunately, there is a small delay before the text is changed. What would be the best way to initialize the textfield into something dynamic? So the myTextField doesn't have to be initialized as static text.
Upvotes: 0
Views: 851
Reputation: 2941
You could create an NSTextField subclass, override the awakeFromNib method and put your code there... Here's some (untested) code:
// MyDateTextField.h
@interface MyDateTextField : NSTextField
@end
// MyDateTextField.m
@implementation MyDateTextField
- (void)awakeFromNib {
NSDate *currentDate = [NSDate date];
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:currentDate];
[myTextField setStringValue:[NSString stringWithFormat:@"Year: %ld", [components year]]];
}
@end
Alternatively, you could post an NSNotification in the awakeFromNib
method, and pick it up in your controller... That might be better design, depending on how much you need your text field to do.
Upvotes: 1