【问题标题】:Add checkbox to NSOpenPanel将复选框添加到 NSOpenPanel
【发布时间】:2012-10-17 18:11:06
【问题描述】:

我想给NSOpenPanel添加一个复选框,然后在接收到选中的文件时查询它的状态。我该怎么做?

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

【问题讨论】:

    标签: macos cocoa nsopenpanel


    【解决方案1】:

    基于 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
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • +1 为我指明了正确的方向。稍后我将发布完整解决方案的答案。
      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2017-02-07
        • 2015-11-22
        • 2015-08-13
        • 2012-07-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多