views:

35

answers:

1

I've got a couple of NSTextFields in my application for entering date and time. They have NSDateFormatters (using NSDateFormatterBehavior10_4) attached to them.

When I change the system date format in System Preferences, then switch back to my application, the text fields automatically update with the new date format, but only if they are enabled. If they're disabled, nothing happens until I enable and tab into them.

How can I trigger this formatter update even if the field is disabled? I've tried setting the object value to itself and using setNeedsDisplay but neither works.

A: 

My problem had to do with the particular notification I was using to detect the format change.

When AppleDatePreferencesChangedNotification was delivered, the locale hadn't updated, so no matter what I tried, Cocoa was updating the field using the old locale information. After a short delay, the locale updated and everything works fine.

I think this is why NSCurrentLocaleDidChangeNotification was added in 10.5, but since I am supporting back to 10.4, I'll use this workaround for now.

    NSDistributedNotificationCenter *distributedNotificationCenter = [NSDistributedNotificationCenter defaultCenter];
    [distributedNotificationCenter addObserver: self selector: @selector(_dateFormatsChanged:) name: @"AppleDatePreferencesChangedNotification" object: nil];

// ...

- (void)_localeChanged;
{
    // ... update stuff ...
}

- (void)_dateFormatsChanged:(NSNotification *)notification;
{
    // XXX delay while NSLocale updates - can we use another notification instead?
    // XXX 10.5+ has NSCurrentLocaleDidChangeNotification
    [self performSelector: @selector(_localeChanged) withObject: nil afterDelay: 0.1];
}
Nicholas Riley