自窗口为空,但窗口插座已正确链接

时间:2011-10-25 10:33:50

标签: objective-c cocoa

在我的NSWindowController的子类中,[self window]为null。

在我的nib文件中,文件所有者(我的子类)和窗口视图之间存在链接。

为什么我会收到此错误?

当我重构文件所有者的类(NSWindowController的子类)时,它停止工作。我已经在笔尖更新了它,所以我不明白为什么它停止工作。

崩溃线:

session = [NSApp beginModalSessionForWindow:[self window]];



   2011-10-25 12:27:14.377 MyApp [13161:b0f] *** Assertion failure in -[CBApplication _commonBeginModalSessionForWindow:relativeToWindow:modalDelegate:didEndSelector:contextInfo:], /SourceCache/AppKit/AppKit-1138.23/AppKit.subproj/NSApplication.m:3861
    2011-10-25 12:27:14.377 MyApp[13161:b0f] An uncaught exception was raised
    2011-10-25 12:27:14.378 MyApp[13161:b0f] Modal session requires modal window
    2011-10-25 12:27:14.380 MyApp[13161:b0f] (
        0   CoreFoundation                      0x92e01d87 __raiseError + 231
        1   libobjc.A.dylib                     0x9317e149 objc_exception_throw + 155
        2   CoreFoundation                      0x92d69619 +[NSException raise:format:arguments:] + 137
        3   Foundation                          0x9c41c36f -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
        4   AppKit                              0x958a987d -[NSApplication _commonBeginModalSessionForWindow:relativeToWindow:modalDelegate:didEndSelector:contextInfo:] + 725
        5   AppKit                              0x958a1973 -[NSApplication beginModalSessionForWindow:] + 72
        6   MyApp                     0x00042ca3 -[CBWindowController showModal:] + 131
        7   MyApp                     0x00023c46 -[CBDocument showLinkWindow:shouldLinkAndUpdate:selectedOnly:] + 1174
        8   MyApp                     0x00023cb1 -[CBDocument linkAllRootItems:] + 81
        9   MyApp                     0x0002a9b4 -[CBApplicationDelegate linkAllItems:] + 100
        10  CoreFoundation                      0x92d57091 -[NSObject performSelector:withObject:] + 65
        11  AppKit                              0x956e1cb3 -[NSApplication sendAction:to:from:] + 232
        12  AppKit                              0x957d5caf -[NSMenuItem _corePerformAction] + 536
        13  AppKit                              0x957d592c -[NSCarbonMenuImpl performActionWithHighlightingForItemAtIndex:] + 171
        14  AppKit                              0x957d4fb5 -[NSMenu _performActionWithHighlightingForItemAtIndex:sendAccessibilityNotification:] + 79
        15  AppKit                              0x95aaddab -[NSMenu performActionForItemAtIndex:] + 65
        16  AppKit                              0x95aaddde -[NSMenu _internalPerformActionForItemAtIndex:] + 45
        17  AppKit                              0x95ab200f -[NSMenuItem _internalPerformActionThroughMenuIfPossible] + 106
        18  AppKit                              0x9591ba10 -[NSCarbonMenuImpl _carbonCommandProcessEvent:handlerCallRef:] + 172
        19  AppKit                              0x9574a916 NSSLMMenuEventHandler + 452
        20  HIToolbox                           0x9b175920 _Z22_InvokeEventHandlerUPPP25OpaqueEventHandlerCallRefP14OpaqueEventRefPvPFlS0_S2_S3_E + 36
        21  HIToolbox                           0x9aff1803 _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 1602
        22  HIToolbox                           0x9aff0c80 _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 482
        23  HIToolbox                           0x9b005aa9 SendEventToEventTarget + 76
        24  HIToolbox                           0x9b175de4 _ZL18SendHICommandEventmPK9HICommandmmhPKvP20OpaqueEventTargetRefS5_PP14OpaqueEventRef + 482
        25  HIToolbox                           0x9b175e4e SendMenuCommandWithContextAndModifiers + 70
        26  HIToolbox                           0x9b1e0697 SendMenuItemSelectedEvent + 275
        27  HIToolbox                           0x9b0423f9 _ZL19FinishMenuSelectionP13SelectionDataP10MenuResultS2_ + 129
        28  HIToolbox                           0x9b1d1574 _ZL14MenuSelectCoreP8MenuData5PointdmPP13OpaqueMenuRefPt + 608
        29  HIToolbox                           0x9b03a0b2 _HandleMenuSelection2 + 636
        30  HIToolbox                           0x9b039e31 _HandleMenuSelection + 53
        31  AppKit                              0x95646356 _NSHandleCarbonMenuEvent + 302
        32  AppKit                              0x955d662e _DPSNextEvent + 2196
        33  AppKit                              0x955d58ab -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 113
        34  AppKit                              0x955d1c22 -[NSApplication run] + 911
        35  AppKit                              0x9586618a NSApplicationMain + 1054
        36  MyApp                     0x000042f4 main + 36
        37  MyApp                     0x00002e06 start + 54

如何创建NSWindowController:

    CBWindowController *windowController = [[subClass alloc] init];
[windowController setRanAsModal:YES];
[windowController setDelegate:self];
[windowController setRootDocument:[NSApp mainWindowDocument]];
[windowController loadWindow];
[windowController centerOnMainWindow:sender];

2 个答案:

答案 0 :(得分:2)

[self window]必须指向有效的窗口对象。从你的评论来看,事实并非如此。

您必须检查并重新连接窗口插座,或者,如果没有这样的插座,请确保该变量中保留有效对象。

自动重构似乎并未捕获所有内容 - 因此,在重构后,搜索项目中的旧名称似乎是值得的,以防止此类问题在未来发生。

答案 1 :(得分:2)

我愿意打赌你错了。初始化类时需要指定NIB名称,如下所示:

CBWindowController *windowController =
    [[subClass alloc] initWithWindowNibName:@"MyWindow" owner:nil];

更好的是,只需将该代码折叠到窗口控制器的init方法中即可。

编辑:另外,不要在对象上调用loadWindow,在访问窗口时会自动调用该方法,如文档中所述。