Presenting modal dialogs from XIB in Cocoa: best/shortest pattern?
Based on Ken Thomases answer given on SO here, here is my revised modal window module. Note that the window should have the system close button disabled, or otherwise you will have to handle windowWillClose, which will lead to having _dialogCode like in my original code above. If, however, OK and Cancel are the only buttons that end the modal dialog, then:

#import "MyPasswordWindowController.h"

@interface MyPasswordWindowController ()
@property (weak) IBOutlet NSSecureTextField *passwordField;
@end

@implementation MyPasswordWindowController

- (IBAction)okCancelAction:(NSButton *)sender
{
    [NSApp stopModalWithCode:sender.tag]; // the OK button's tag should be 1
    [self.window close];
}

+ (NSString*)run
{
    MyPasswordWindowController* windowController =
        [[MyPasswordWindowController alloc] initWithWindowNibName:@"MyPassword"];
    if ([NSApp runModalForWindow:windowController.window])
        return windowController.passwordField.stringValue;
    return nil;
}

@end