【问题标题】:NSPanel causes subsequent use of NSOpenPanel to generate an exceptionNSPanel 导致后续使用 NSOpenPanel 产生异常
【发布时间】:2015-03-18 22:00:36
【问题描述】:

我有一个使用 NSOpenPanel 的应用程序,以便用户可以选择多个文件进行导入,并且还有一个典型的首选项面板。首选项面板存储在.xib 文件中。

用户调用首选项面板后出现问题。在此之前 NSOpenPanel 工作得非常好,所有逻辑都在完成块中执行,并且可以一次又一次地重新调用。但是,一旦用户选择了 Preferences 选项并关闭了该表,那么随后对 -(IBAction)addFiles: 的每次调用都会导致异常:

它们是这样声明的:

@interface BQSAppDelegate
{
    NSOpenPanel *addFilesPanel;
}
@property(assign)IBOutlet NSPanel*prefs_panel; //connected to a NSPanel in the .xib

并使用:

@implementation BQSAppDelegate

-(void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// other initialisations not to do with addFilesPanel….
    addFilesPanel = [NSOpenPanel openPanel];
    addFilesPanel.canChooseDirectories = YES;
    addFilesPanel.allowsMultipleSelection = YES;
}

-(IBAction)addFiles:(id)sender //button on main window pressed
{
    [addFilesPanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
        if (addFilesPanel.URLs.count < 1 || result != NSFileHandlingPanelOKButton) {
            NSLog(@“User cancelled”);
            [addFilesPanel orderOut:self]; //I’ve tried it without this line with exactly the same results
        }
        else {
            //do things…
        }
    }];
}

-(IBAction)preferences:(id)sender //bound to the usual Apple+’,” shortcut and app menu item
{
    if (self.prefs_panel.isVisible) {
        return;
    }
    [self.window beginSheet:self.prefs_panel completionHandler:^(NSModalResponse returnCode) {
        NSLog(@"completed"); //is never executed...
    }];
}

-(IBAction)ok_prefs:(id)sender //called when the user presses a button on the Preference panel
{    
    [self.prefs_panel orderOut:self];
}

例外是:

2015-03-18 20:35:29.913 QS[3634:241869] *** Assertion failure in void associateSheetWithHost(NSWindow *, NSWindow *)(), /SourceCache/ViewBridge/ViewBridge-103/ViewBridgeUtilities.m:83
2015-03-18 20:35:29.914 QS[3634:241869] sheet <NSVBOpenPanel: 0x1003504f0> already has a host <NSWindow: 0x6000001e5e00>
2015-03-18 20:35:29.917 QS[3634:241869] (
    0   CoreFoundation                      0x00007fff95e7366c __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x00007fff8db6776e objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff95e7344a +[NSException raise:format:arguments:] + 106
    3   Foundation                          0x00007fff9399e471 -[NSAssertionHandler handleFailureInFunction:file:lineNumber:description:] + 169
    4   ViewBridge                          0x00007fff8c931333 -[NSWindow(ViewBridgeNeedsSPI) _setSheetHost:] + 305
    5   AppKit                              0x00007fff8b367a2f -[NSVBSavePanel beginSheetModalForWindow:completionHandler:] + 69
    6   QS                          0x0000000100017684 -[BQSAppDelegate addFolders:] + 212
    7   libsystem_trace.dylib               0x00007fff8be72cd7 _os_activity_initiate + 75
    8   AppKit                              0x00007fff8acd8b71 -[NSApplication sendAction:to:from:] + 452
    9   AppKit                              0x00007fff8acd8970 -[NSControl sendAction:to:] + 86
    10  AppKit                              0x00007fff8aeae86c __26-[NSCell _sendActionFrom:]_block_invoke + 131
    11  libsystem_trace.dylib               0x00007fff8be72cd7 _os_activity_initiate + 75
    12  AppKit                              0x00007fff8ad21509 -[NSCell _sendActionFrom:] + 144
    13  libsystem_trace.dylib               0x00007fff8be72cd7 _os_activity_initiate + 75
    14  AppKit                              0x00007fff8ad3c085 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2775
    15  AppKit                              0x00007fff8ad3b2b9 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 491
    16  AppKit                              0x00007fff8ad3a899 -[NSControl mouseDown:] + 714
    17  AppKit                              0x00007fff8b238a18 -[NSWindow _reallySendEvent:] + 12721
    18  AppKit                              0x00007fff8acbf16e -[NSWindow sendEvent:] + 446
    19  AppKit                              0x00007fff8ac71451 -[NSApplication sendEvent:] + 4183
    20  AppKit                              0x00007fff8aafd608 -[NSApplication run] + 711
    21  AppKit                              0x00007fff8aae8a14 NSApplicationMain + 1832
    22  QS                          0x0000000100001fe2 main + 34
    23  libdyld.dylib                       0x00007fff90d485c9 start + 1

这一定与我调用和/或关闭 NSPanel 的方式有关,但我不知道是什么。非常感谢您的帮助。

【问题讨论】:

  • 我认为你必须在某个地方调用 endSheet 才能调用完成块。
  • 谢谢史蒂夫,我会试一试的。
  • stevesliva:这似乎已经解决了!做得好!我一直在错误地忽略它,这显然是导致此异常的原因。你能把你的评论作为答案,我会标记它。

标签: macos cocoa nsopenpanel nspanel


【解决方案1】:

这是一条神秘的错误消息,尽管评论解决了问题,但我不确定我是否理解它的措辞。我在想这与存在与工作表相关联的模态运行循环有关,并且它可能与此有关——也许模态会话的所有开销仍然在某个地方活跃着,除非你打电话给endSheet

提示来自this related question about the completion handler not being invoked. 第二个未被接受的答案似乎表明您必须终止模态会话,而不仅仅是终止工作表。

【讨论】:

  • 恕我直言,自从 OSX 上的沙盒以来,隐秘的错误消息并非闻所未闻。确实有所不同:自从我更改了代码几天后,一切都很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-07
  • 1970-01-01
  • 2019-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-17
相关资源
最近更新 更多