In a Mac OS X Cocoa application, I have an application-modal dialog with text fields that are bound to the Shared User Defaults Controller. If I edit a text field, and then tab away from it before hitting the OK button, then everything works as desired. However, if I start editing a field, then hit the Return key to trigger OK, the old value of the field remains in NSUserDefaults.
So, how can I force the changed field to affect the bound value when editing is ‘incomplete’?
From perusing documentation, I think I might be able to call NSControl’s validateEditing method for each of the text fields before dismissing the dialog, but it seems like there should be a simpler way.
FWIW, here is the code that displays the dialog:
- (void)showDialog { [NSApp activateIgnoringOtherApps:YES]; [NSApp beginSheet:startTimerDialog modalForWindow:nil modalDelegate:nil didEndSelector:nil contextInfo:nil]; [NSApp runModalForWindow:startTimerDialog]; [NSApp endSheet:startTimerDialog]; [startTimerDialog orderOut:self]; }
The OK button (actually titled ‘Start’) is targetted to this method:
- (IBAction)startTimerDialogStartButtonWasClicked:(id)sender { [self closeModalDialog:sender]; // Then, call methods that read values from NSUserDefaults // ... }
Send the user defaults controller a
commitEditingmessage.