Cocoa uses custom dialog box method

The dialog box is actually a window. We create a xib file in interface builder, drag in a window, and then design our dialog box. After the interface is designed, we need code to control the behavior of the dialog box, such as when to display and when to hide As well as some event responses, NSWindowController can be used at this time.

Create a new NSWindowController subclass DialogController, set the fileOwner in the newly created xib file to the DialogController type in the interface bulider, pull the line to associate the window in FileOwner with the dialog window, and the association of other controls, and then add the logic you need to DialogController Code, such a complete dialog box design is complete. Then you can call your custom dialog box in the AppControlelr of your program, define and initialize a DialogController type object, as follows:

DialogController* dialogCtl;

dialogCtl = [[DialogController alloc] initWithWindowNibName:@"DialogName"]; // DialogName is your xib file name, no suffix is ​​required

[dialogCtl loadWindow];

There are two ways to display dialog boxes in Mac, one is the same as the windows dialog box, and the other is the Sheet (roll-up) dialog box.

Windows style dialog box, divided into modal and non-modal

Non-modal: [[dialogCtl window] makeKeyAndOrderFront:nil];

模态:[NSApp runModalForWindow:[dialogCtl window]];


The sheet dialog boxes are all modal dialog boxes, and the display mode:

NSApp beginSheet:[dialogCtl window]
       modalForWindow:[NSApp mainWindow]
        modalDelegate:nil
       didEndSelector:nil
          contextInfo:nil];
    
[NSApp runModalForWindow:[dialogCtl window]];

If you use a sheet dialog box, when the dialog box exits, you need to call the following code in the DialogController:

[NSApp stopModal];
[NSApp endSheet:[self window]];
[[self window] orderOut:nil];

Otherwise, although the dialog box disappears, the main window is still in a modal state and cannot respond. Clicking back will cause a "pop" sound, you know.

Guess you like

Origin blog.csdn.net/qq_27740983/article/details/50242529