Domanda

I would like to add a checkbox to NSOpenPanel, and then query its state when receiving the selected files. How can I do this?

Additionally, it would be desirable to be able to enable or disable the checkbox based on the current file selection.

È stato utile?

Soluzione

The complete solution based on the answers of Joshua Nozzi and Mark Alldritt:

NSOpenPanel* openDlg = [NSOpenPanel openPanel];
NSButton *button = [[NSButton alloc] init];
[button setButtonType:NSSwitchButton];
button.title = NSLocalizedString(@"I am a checkbox", @"");
[button sizeToFit];
[openDlg setAccessoryView:button];
openDlg.delegate = self;
[openDlg beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) 
{
    openDlg.delegate = nil; // TODO: Check if this is necessary
    if (result != NSFileHandlingPanelOKButton) return;
    BOOL checkboxOn = (((NSButton*)openDlg.accessoryView).state == NSOnState); 
    // Do something
}];

The NSOpenSavePanelDelegate:

- (void)panelSelectionDidChange:(id)sender {
    NSOpenPanel *panel = sender;
    NSButton *button = (NSButton*)panel.accessoryView;
    // Update button based on panel selection
}

Altri suggerimenti

NSOpenPanel is a subclass of NSSavePanel, which has -setAccessoryView:.

To validate your checkbox based on the selected file, you need to implement panelSelectionDidChange: from the NSOpenSavePanelDelegate delegate protocol. You can then query the the open panel's currently selected file(s) and update your checkbox state as needed.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top