2

我是 Mac 应用程序编程的新手。我正在使用 NSOpenPanel 从系统中选择一个文件。一旦我选择了一个文件,然后按取消/打开按钮,completionHandler 块就会被执行。

当 OpenPanel 窗口关闭时,我面临以下问题:

  1. 即使 mainAppWindow.visible 的结果为 1,我的主应用程序窗口也将关闭
  2. 另外,我没有收到主 App 窗口的窗口关闭委托方法。

请在下面找到我用来创建 NSOpenPanel 的代码片段。

-(void)openFile
{
    //this gives you a copy of an open file dialogue
    NSOpenPanel*openPanel = [NSOpenPanel openPanel];

    //set the title of the dialogue window
    openPanel.title = @"Choose a .zip formate file";

    //shoud the user be able to resize the window?
    openPanel.showsResizeIndicator = NO;

    //should the user see hidden files (for user apps - usually no)
    openPanel.showsHiddenFiles = NO;

    //can the user select a directory?
    openPanel.canChooseDirectories = NO;

    //can the user create directories while using the dialogue?
    openPanel.canCreateDirectories = NO;

    //should the user be able to select multiple files?
    openPanel.allowsMultipleSelection = NO;

    //an array of file extensions to filter the file list
    openPanel.allowedFileTypes = @[@"zip"];

    [openPanel beginWithCompletionHandler:^(NSInteger result) {

        //if the result is NSOKButton
        //the user selected a file

            if (result==NSModalResponseOK) {

                //Do something
            }
            else if (result== NSModalResponseCancel)
            {
                //do something
            }
    }];  
}
4

0 回答 0