按下NSToolbarItem时定位弹出窗口

时间:2014-02-21 07:38:14

标签: objective-c macos cocoa nspopover

我想在用户点击NSToolbarItem时显示一个Popover。但是,在我的IBAction中,我没有为我的发件人提供NSView个实例,我觉得这很奇怪。

我开始显示Popover(现在通过使用window.contentView作为我的视图而不是工具栏来显示错位 - 见图片):

- (IBAction)accountIconTouched:(NSToolbarItem *)sender {

    // NSView *view = [sender view]; // returns nil
    NSView *view = self.window.contentView;

    [self.accountPopover showRelativeToRect:[view bounds] ofView:view preferredEdge:NSMinYEdge];


}

那么如何获取NSToolbarItem的视图以便Popover显示出来。

enter image description here

这是我从控制台得到的错误:

2014-02-20 13:19:16.605 MyProject[42514:303] -[NSPopover showRelativeToRect:ofView:preferredEdge:]: nil view provided. You must supply a view.
2014-02-20 13:19:16.607 MyProject[42514:303] (
    0   CoreFoundation                      0x00007fff8086e41c __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x00007fff8bf33e75 objc_exception_throw + 43
    2   AppKit                              0x00007fff8cbf7b9c -[NSPopover showRelativeToRect:ofView:preferredEdge:] + 2214
    3   MyProject                           0x000000010000306d -[AppDelegate accountIconTouched:] + 253
    4   AppKit                              0x00007fff8d086e19 -[NSToolbarButton sendAction:to:] + 75
    5   AppKit                              0x00007fff8d086e68 -[NSToolbarButton sendAction] + 65
    6   AppKit                              0x00007fff8cb713d0 -[NSApplication sendAction:to:from:] + 327
    7   AppKit                              0x00007fff8cc3be9e -[NSToolbarItemViewer mouseDown:] + 4771
    8   AppKit                              0x00007fff8cb57d08 -[NSWindow sendEvent:] + 11296
    9   AppKit                              0x00007fff8caf6744 -[NSApplication sendEvent:] + 2021
    10  Afloat                              0x00000001007c2479 Afloat + 13433
    11  AppKit                              0x00007fff8c946a29 -[NSApplication run] + 646
    12  AppKit                              0x00007fff8c931803 NSApplicationMain + 940
    13  MyProject                           0x0000000100001912 main + 34
    14  libdyld.dylib                       0x00007fff8b1635fd start + 1
    15  ???                                 0x0000000000000003 0x0 + 3
)

1 个答案:

答案 0 :(得分:2)

似乎有一个解决方案,您可以将IBOutlet分配给工具栏项,并从那里获取视图。试一试here

enter image description here