我得到以下错误和堆栈跟踪,但是视图不是nil(正如它之前的记录所示)
堆栈跟踪
2014-02-05 08:29:47.338 MenuTimer[74522:303] An uncaught exception was raised
2014-02-05 08:29:47.338 MenuTimer[74522:303] The contentViewController ((null)) or contentViewController.view is nil.
2014-02-05 08:29:47.340 MenuTimer[74522:303] (
0 CoreFoundation 0x00007fff8e39341c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff87b1ee75 objc_exception_throw + 43
2 CoreFoundation 0x00007fff8e3932cc +[NSException raise:format:] + 204
3 AppKit 0x00007fff8157e3da -[NSPopover showRelativeToRect:ofView:preferredEdge:] + 228
4 MenuTimer 0x0000000100002a72 -[MenuTimer onSetTime:] + 818
5 AppKit 0x00007fff814f83d0 -[NSApplication sendAction:to:from:] + 327
6 AppKit 0x00007fff81513348 -[NSMenuItem _corePerformAction] + 394
7 AppKit 0x00007fff81513084 -[NSCarbonMenuImpl performActionWithHighlightingForItemAtIndex:] + 117
8 AppKit 0x00007fff815624dd -[NSMenu _internalPerformActionForItemAtIndex:] + 35
9 AppKit 0x00007fff81562359 -[NSCarbonMenuImpl _carbonCommandProcessEvent:handlerCallRef:] + 104
10 AppKit 0x00007fff81509176 NSSLMMenuEventHandler + 716
11 HIToolbox 0x00007fff8acff6d4 _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 892
12 HIToolbox 0x00007fff8acfec87 _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 385
13 HIToolbox 0x00007fff8ad12d90 SendEventToEventTarget + 40
14 HIToolbox 0x00007fff8ad48a30 _ZL18SendHICommandEventjPK9HICommandjjhPKvP20OpaqueEventTargetRefS5_PP14OpaqueEventRef + 420
15 HIToolbox 0x00007fff8ad7b618 SendMenuCommandWithContextAndModifiers + 59
16 HIToolbox 0x00007fff8ad7b5c4 SendMenuItemSelectedEvent + 178
17 HIToolbox 0x00007fff8ad7b4a5 _ZL19FinishMenuSelectionP13SelectionDataP10MenuResultS2_ + 94
18 HIToolbox 0x00007fff8ad5956e _ZL19PopUpMenuSelectCoreP8MenuData5PointdS1_tjPK4RecttjS4_S4_PK10__CFStringPP13OpaqueMenuRefPt + 1726
19 HIToolbox 0x00007fff8ad586c6 _HandlePopUpMenuSelection7 + 596
20 AppKit 0x00007fff81561221 _NSSLMPopUpCarbonMenu3 + 4153
21 AppKit 0x00007fff815601d6 _NSPopUpCarbonMenu3 + 39
22 AppKit 0x00007fff8155fe96 -[NSCarbonMenuImpl popUpMenu:atLocation:width:forView:withSelectedItem:withFont:withFlags:withOptions:] + 350
23 AppKit 0x00007fff81950959 +[NSStatusBarButtonCell popupStatusBarMenu:inRect:ofView:withEvent:] + 699
24 AppKit 0x00007fff81951ade -[NSStatusItem popUpStatusItemMenu:] + 173
25 MenuTimer 0x0000000100001779 -[StatusBarView mouseDown:] + 633
26 AppKit 0x00007fff814ded08 -[NSWindow sendEvent:] + 11296
27 AppKit 0x00007fff815da11b -[NSStatusBarWindow sendEvent:] + 247
28 AppKit 0x00007fff8147d744 -[NSApplication sendEvent:] + 2021
29 AppKit 0x00007fff812cda29 -[NSApplication run] + 646
30 MenuTimer 0x0000000100002500 -[MenuTimer init] + 2112
31 MenuTimer 0x00000001000031b5 main + 101
32 libdyld.dylib 0x00007fff87fcc5fd start + 1
)
但是,以下代码和输出表示不同。是因为我没有明确的控制器吗?
代码
// create set time window
NSLog(@"Set Time...");
NSLog(@"View %@", self.statusTime.view);
NSLog(@"Frame %@", self.statusTime.view.frame);
NSPopover *popover = [[NSPopover alloc] init];
NSLog(@"Showing popover...");
[popover showRelativeToRect:[self.statusTime.view frame]
ofView:self.statusTime.view
preferredEdge:NSMinYEdge];
输出
2014-02-05 08:29:47.237 MenuTimer[74522:303] Set Time...
2014-02-05 08:29:47.237 MenuTimer[74522:303] View <StatusBarView: 0x6080001b05a0>
2014-02-05 08:29:47.238 MenuTimer[74522:303] Frame <StatusBarView: 0x6080001b05a0>
2014-02-05 08:29:47.238 MenuTimer[74522:303] Showing popover...
答案 0 :(得分:2)
在显示之前,您需要设置contentViewController(popover实际显示的内容)。
即
NSViewController *viewController; // some view controller created programmatically or from a nib
NSPopover *popover = [[NSPopover alloc] init];
popover.contentViewController = viewController; // Missing this.
NSLog(@"Showing popover...");
[popover showRelativeToRect:[self.statusTime.view frame]
ofView:self.statusTime.view
preferredEdge:NSMinYEdge];
ofView:
参数就是弹出窗口的显示位置。