添加复选框到NSOpenPanel

时间:2012-10-17 18:11:06

标签: macos cocoa nsopenpanel

我想在NSOpenPanel添加一个复选框,然后在收到所选文件时查询其状态。我怎么能这样做?

此外,希望能够根据当前文件选择启用或禁用复选框。

3 个答案:

答案 0 :(得分:7)

基于Joshua Nozzi和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
}];

NSOpenSavePanelDelegate:

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

答案 1 :(得分:3)

NSOpenPanel是NSSavePanel的子类,它有-setAccessoryView:

答案 2 :(得分:3)

要根据所选文件验证您的复选框,您需要从panelSelectionDidChange:委托协议实施NSOpenSavePanelDelegate。然后,您可以查询打开面板的当前选定文件,并根据需要更新复选框状态。