Question

I want to show a Popover when the user clicks on a NSToolbarItem. However, in my IBAction I do not get a NSView instance for my sender, which I find really weird.

I get to display the Popover (rather dislocated now by using window.contentView as my view instead of the toolbar's - see image):

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

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

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


}

So how do I get the view for my NSToolbarItem so that the Popover shows up.

enter image description here

This is the error I get from the console:

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
)
Était-ce utile?

La solution

It seems like there's a solution where you assign an IBOutlet to your toolbar Item, and get the view from there. To give it a try check here.

enter image description here

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top