gpt4 book ai didi

cocoa - 如何以编程方式创建 NSPopover

转载 作者:行者123 更新时间:2023-12-03 16:04:37 24 4
gpt4 key购买 nike

我没有找到一些动态创建 NSPopover 的示例,而是使用 Interface Builder。

以下代码出现异常,已加载 Nib ,但未设置 View :

NSViewController *controller = [[NSViewController alloc] initWithNibName:@"View"     bundle:nil];
NSPopover *popover = [[NSPopover alloc] init];
[popover setContentSize:NSMakeSize(100.0f, 100.0f)];
[popover setContentViewController:controller];
[popover setAnimates:YES];
[popover showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMaxXEdge];
[popover release];
[controller release];

这是堆栈跟踪:

2011-10-22 12:00:16.804 Test[2020:707] -[NSViewController loadView] loaded the "View" nib    but no view was set.
2011-10-22 12:00:16.807 Test[2020:707] (
0 CoreFoundation 0x00007fff87e10286 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff92f2cd5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff87e100ba +[NSException raise:format:arguments:] + 106
3 CoreFoundation 0x00007fff87e10044 +[NSException raise:format:] + 116
4 AppKit 0x00007fff90951e21 -[NSViewController loadView] + 336
5 AppKit 0x00007fff9094da8a -[NSViewController view] + 41
6 AppKit 0x00007fff91065232 -[NSPopover showRelativeToRect:ofView:preferredEdge:] + 159
7 Test 0x000000010000159a -[OITAppDelegate show:] + 570
8 CoreFoundation 0x00007fff87dffa1d -[NSObject performSelector:withObject:] + 61
9 AppKit 0x00007fff90924710 -[NSApplication sendAction:to:from:] + 139
10 AppKit 0x00007fff90924642 -[NSControl sendAction:to:] + 88
11 AppKit 0x00007fff9092456d -[NSCell _sendActionFrom:] + 137
12 AppKit 0x00007fff90923a30 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2014
13 AppKit 0x00007fff909a38e0 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 489
14 AppKit 0x00007fff9092263a -[NSControl mouseDown:] + 786
15 AppKit 0x00007fff908ed0e0 -[NSWindow sendEvent:] + 6306
16 AppKit 0x00007fff9088568f -[NSApplication sendEvent:] + 5593
17 AppKit 0x00007fff9081b682 -[NSApplication run] + 555
18 AppKit 0x00007fff90a9a80c NSApplicationMain + 867
19 Test 0x00000001000012f2 main + 34
20 Test 0x00000001000012c4 start + 52
21 ??? 0x0000000000000003 0x0 + 3

最佳答案

问题不在于 NSPopover; NSViewController 抛出异常,它表示虽然 nib 加载成功,但它的 view 属性仍然是 nil

在 Interface Builder 中打开“View.nib”并将文件所有者view 导出分配给 Controller 应该代表的 View 对象(NSViewContoller 成为文件加载 Nib 时的所有者)。这是通过按住 Ctrl 键从文件所有者图标拖动到 View 图标,然后从导出列表中选择 View 来完成的。

关于cocoa - 如何以编程方式创建 NSPopover,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7858765/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com