[MBS]Popover with button

I’m working with the example “Statusitem with PopOver using Container.xojo_binary_project”, which shows a popover for a statusitem. I want to show a popover when a button is clicked. Therefore, I moved the main code to the action event of a button:

[code] if pvrTask <> nil then
pvrTask.performClose
pvrTask = nil
vcTask = nil
end if

	vcTask = new NSViewControllerMBS
	vcTask.view = conMain.NSViewMBS
	
	pvrTask = new NSPopoverMBS
	pvrTask.animates = true
	pvrTask.contentViewController = vcTask
	pvrTask.behavior = pvrTask.NSPopoverBehaviorTransient
	'pvrTask.appearance = pvrTask.NSPopover
	
	dim f as NSRectMBS = self.frame 'error here
	dim r as NSRectMBS = NSMakeRectMBS(0, 0, f.Width, f.Height)
	dim v as NSViewMBS = self.contentView 'error here
	
	pvrTask.showRelativeToRect(r, v, pvrTask.MaxYEdge)[/code]

But how do I need to change the code to make it work in a window? The line with “dim f” gives me a TypeMismatchError. “dim v” and following has “window1 has no member contentview”.

you get the NSWindow for the Xojo window:

dim w as NSWindowMBS = self.NSWindowMBS

and than use the w instead of the self.

Thanks, Christian.

Next problem:

vcTask.view = conMain.NSViewMBS

gives me a NOE because conMain isn’t instantiated. But if I use

vcTask.view = ContainerControl1.NSViewMBS

instead I get “this item doesn’t exist”.

well, you need to make an instance of the container with the controls.
And than you can move the view from the window to the popover. That is a trick we use in the example.

Okay, for testing I put the container on the window and then instantiate with:

vcTask.view = ContainerControl11.NSViewMBS

But - just to be very annoying - I now get a hard crash when quitting the app:

Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x0000000000000000
Exception Note: EXC_CORPSE_NOTIFY

VM Regions Near 0:
–>
__TEXT 0000000000001000-0000000000210000 [ 2108K] r-x/rwx SM=COW /Users/USER/Documents/*/Statusitem with textfield in menubar.debug.app/Contents/MacOS/Statusitem with textfield in menubar.debug

Application Specific Information:
objc_msgSend() selector name: respondsToSelector:

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libobjc.A.dylib 0x93701cb3 objc_msgSend + 35
1 com.apple.AppKit 0x92ee1b9f _NSPopoverClosePopoversStartingWithWindow + 813
2 com.apple.AppKit 0x92ee195c _NSPopoverClosePopoversStartingWithWindow + 234
3 com.apple.AppKit 0x92ee238a ___NSPopoverProcessResignKey_block_invoke + 58
4 com.apple.CoreFoundation 0x9d2a9da0 CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK + 16
5 com.apple.CoreFoundation 0x9d287873 __CFRunLoopDoBlocks + 195
6 com.apple.CoreFoundation 0x9d286fb2 __CFRunLoopRun + 978
7 com.apple.CoreFoundation 0x9d286976 CFRunLoopRunSpecific + 390
8 com.apple.CoreFoundation 0x9d2867db CFRunLoopRunInMode + 123
9 com.apple.AppKit 0x9271b5f0 _NSUnhighlightCarbonMenu + 355
10 com.apple.AppKit 0x92714ab6 -[NSCarbonMenuImpl performActionWithHighlightingForItemAtIndex:] + 182
11 com.apple.AppKit 0x927140e3 -[NSMenu _performActionWithHighlightingForItemAtIndex:sendAccessibilityNotification:] + 79
12 com.apple.AppKit 0x927b00cb __38-[NSMenu performActionForItemAtIndex:]_block_invoke + 52
13 libsystem_trace.dylib 0x9e5f63c1 _os_activity_initiate + 85
14 com.apple.AppKit 0x927b008f -[NSMenu performActionForItemAtIndex:] + 141
15 com.apple.AppKit 0x927afff8 -[NSMenu _internalPerformActionForItemAtIndex:] + 45
16 com.apple.AppKit 0x927affc3 -[NSMenuItem _internalPerformActionThroughMenuIfPossible] + 106
17 com.apple.AppKit 0x927afe1a -[NSCarbonMenuImpl _carbonCommandProcessEvent:handlerCallRef:] + 120
18 com.apple.AppKit 0x926410e9 NSSLMMenuEventHandler + 808
19 com.apple.HIToolbox 0x9de832ff _InvokeEventHandlerUPP(OpaqueEventHandlerCallRef*, OpaqueEventRef*, void*, long ()(OpaqueEventHandlerCallRef, OpaqueEventRef*, void*)) + 36
20 com.apple.HIToolbox 0x9de2b7b0 DispatchEventToHandlers(EventTargetRec*, OpaqueEventRef*, HandlerCallRec*) + 1832
21 com.apple.HIToolbox 0x9de2a9c4 SendEventToEventTargetInternal(OpaqueEventRef*, OpaqueEventTargetRef*, HandlerCallRec*) + 402
22 com.apple.HIToolbox 0x9de3db6d SendEventToEventTarget + 34
23 com.apple.HIToolbox 0x9dfdb394 SendHICommandEvent(unsigned long, HICommand const*, unsigned long, unsigned long, unsigned char, void const*, OpaqueEventTargetRef*, OpaqueEventTargetRef*, OpaqueEventRef**) + 477
24 com.apple.HIToolbox 0x9deb0053 SendMenuCommandWithContextAndModifiers + 72
25 com.apple.HIToolbox 0x9deaffff SendMenuItemSelectedEvent + 207
26 com.apple.HIToolbox 0x9deafec5 FinishMenuSelection(SelectionData*, MenuResult*, MenuResult*) + 135
27 com.apple.HIToolbox 0x9e02c47e MenuSelectCore(MenuData*, Point, double, unsigned long, OpaqueMenuRef**, unsigned short*) + 759
28 com.apple.HIToolbox 0x9deb0583 _HandleMenuSelection2 + 507
29 com.apple.HIToolbox 0x9deb037f _HandleMenuSelection + 55
30 com.apple.AppKit 0x9262b0c5 _NSHandleCarbonMenuEvent + 357
31 com.apple.AppKit 0x9248d69f _DPSNextEvent + 2010
32 com.apple.AppKit 0x9248c85b -[NSApplication _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1057
33 com.apple.AppKit 0x9248c432 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 121
34 XojoFramework 0x002f68bb 0x2b4000 + 272571
35 XojoFramework 0x002f6904 0x2b4000 + 272644
36 Statusitem with textfield in menubar.debug 0x000c8ab4 Delegate.Invoke%% + 34
37 Statusitem with textfield in menubar.debug 0x000404bb Application._CallFunctionWithExceptionHandling%%op + 264
38 XojoFramework 0x0046274a 0x2b4000 + 1763146
39 XojoFramework 0x002f685c 0x2b4000 + 272476
40 com.apple.AppKit 0x9247fb47 -[NSApplication run] + 1063
41 XojoFramework 0x004627ea 0x2b4000 + 1763306
42 XojoFramework 0x004609c4 RuntimeRun + 49
43 Statusitem with textfield in menubar.debug 0x000a8136 REALbasic._RuntimeRun + 34
44 Statusitem with textfield in menubar.debug 0x001d0d0d _Main + 295
45 Statusitem with textfield in menubar.debug 0x001d0785 main + 36
46 Statusitem with textfield in menubar.debug 0x001e38fe start + 53

MacOS 10.11.6. Xojo version next to latest fc, MBS from 26.09.

Edit: need to close the popup with

if pvrTask <> nil then pvrTask.performClose pvrTask = nil vcTask = nil end if

on close.