Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing Reading of a Text Field Before Dismissing Modal Dialog in Cocoa

Tags:

macos

cocoa

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
    // ...    
}
like image 975
Kristopher Johnson Avatar asked Nov 20 '25 00:11

Kristopher Johnson


1 Answers

Send the user defaults controller a commitEditing message.

like image 128
Peter Hosey Avatar answered Nov 22 '25 18:11

Peter Hosey