문제

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