首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSSavePanel收到警告:“请使用NSSavePanel而不是NSApplication或NSWindow启动面板”

NSSavePanel收到警告:“请使用NSSavePanel而不是NSApplication或NSWindow启动面板”
EN

Stack Overflow用户
提问于 2020-08-27 19:13:22
回答 1查看 1.1K关注 0票数 0

我的应用程序运行在用Xcode 11.1构建的macOS 10.15.6中,当我显示一个保存对话框时,我的应用程序突然(至少我刚刚注意到)在控制台上吐出了这些警告:

代码语言:javascript
复制
WARNING: <NSSavePanel: 0x100e43250> found it necessary to prepare implicitly; please prepare panels using NSSavePanel rather than NSApplication or NSWindow.
WARNING: <NSSavePanel: 0x100e43250> found it necessary to start implicitly; please start panels using NSSavePanel rather than NSApplication or NSWindow.
WARNING: <NSSavePanel: 0x100e43250> running implicitly; please run panels using NSSavePanel rather than NSApplication.

问题是,我正在使用NSSavePanel来呈现这个对话框。这是整个方法。我看不出是怎么回事。我没有使用任何不推荐的方法。(在我遇到问题的情况下,windownil,所以调用runModel。)

代码语言:javascript
复制
/**  Present a modal dialog (window==nil) or begin a sheet (window!=nil) that prompts the user to create a new archive.
 *
 * Attaches the rundancy picker view to the save panel so the user can choose the redundancy level.
 * Presents a warning if the user picks a volume that has size limitations.
 * If successful, invokes the handler with the URL of the new repository and its creation settings.
 * @param window window for sheet, or nil for modal dialog
 * @param title optional title
 * @param name optional default name
 * @param directory optional default directory
 * @param handler completion handler
 */
+ (void)newArchivePrompForWindow:(nullable NSWindow *)window
                       withTitle:(nullable NSString*)title
                            name:(nullable NSString*)name
                     inDirectory:(nullable NSURL*)directory
               completionHandler:(void (^)(NSURL* repositoryURL, NSDictionary* settings))handler
{
    NSSavePanel* savePanel = [NSSavePanel savePanel];
    
    savePanel.title = ( title ?: @"New Archive" );
    savePanel.allowedFileTypes = @[kRepositoryFileType];
//  savePanel.canSelectHiddenExtension = YES;
    savePanel.extensionHidden = YES;
    savePanel.prompt = @"Create";
    savePanel.nameFieldLabel = @"Archive:";
    if (directory!=nil)
        savePanel.directoryURL = directory;     // set optional default folder
    if (name!=nil)
        savePanel.nameFieldStringValue = name;  // set optional name
    
    // attach the redundancy picker controller
    RepositoryPickRedundancyViewController* redundancyAccessoryController;
    redundancyAccessoryController = [[RepositoryPickRedundancyViewController alloc] initWithNibName:@"RepositoryPickRedundancyView"
                                                                                             bundle:nil];
    [savePanel setAccessoryView:redundancyAccessoryController.view];
    redundancyAccessoryController.version = kDataRedundancyPickerSchemeDefault;
    redundancyAccessoryController.configurationIndex = kDataRedundancyPickerConfigIndexDefault;
    
    // Present the panel
    if (window!=nil)
        {
        // The dialog is attached to a window
        // Present a sheet attached to the window
        [savePanel beginSheetModalForWindow:window completionHandler:^(NSInteger result) {
            if (result==NSFileHandlingPanelOKButton)
                // User picked an archive: vet it and continue
                NewArchivePostProcessing(savePanel,window,redundancyAccessoryController,savePanel.URL,handler);
            else
                // canceled
                handler(nil,nil);
            }];
        }
    else
        {
        // No parent window: run a modal dialog
        if ([savePanel runModal]==NSModalResponseOK)
            // User picked an archive: vet it and continue
            NewArchivePostProcessing(savePanel,nil,redundancyAccessoryController,savePanel.URL,handler);
        else
            // canceled
            handler(nil,nil);
        }
}

有人知道这是怎么回事吗?

EN

回答 1

Stack Overflow用户

发布于 2020-08-28 18:35:17

如果你换掉

代码语言:javascript
复制
if ([savePanel runModal]==NSModalResponseOK)
    ...

使用

代码语言:javascript
复制
[savePanel beginWithCompletionHandler:^(NSModalResponse result) {
    if (result==NSFileHandlingPanelOKButton)
        ...
    }];

信息就会消失。

我想这是鼓励使用更现代API的一种微妙的方式。

唯一的问题是,消息是错误的,runModal不受欢迎,而且在任何地方都没有提到或记录这一点。这些天来,这似乎是正常的。:(

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63622786

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档