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.

有帮助吗?

解决方案

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
}

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top