NSPopover showing also NSViewController

Hello


for some textfield, I need to show a NSViewController through a popover, when this textfield becomes firstResponder.

My problem is that the viewController displays twice: once as an independant window and once in the popover.


I feel it's because I create the view using:

let wc = helpViewController()

and later:

myPopover.contentViewController = wc


So I've tried this:

let popoverViewController = storyboard?.instantiateController(withIdentifier: "helpViewController")

and

myPopover.contentViewController = popoverViewController


but ly application crashes on the nstantiateController. I can't find helpViewController.


Hoping i have told clearly about my problem, could you help me?

Replies

I assume it is OSX.


How do you detect textField becomes first responder ? With shouldBeginEditing ?


I do not see if you declared the popover in IB, creating NSPopoverController (in fact NSViewController) and NSPopover and link the NSPopover object to an IBOutlet (myPopover).


The way I do this :

- In IB, create a popover object : that creates both NSPopoverController and NSPopover

- create a customView, that will be the content of the popover

- connect the PopoverViewController to this view (select the option "view"

- connect the popover to an IBOutlet myPopover


Then I call the popover with

myPopover.show(relativeTo:, of:, preferredEdge: NSRectEdge.MaxX)

The method becomeFirstResponder of an NSTextField is called when a textfield becomes first Responder


for he popover, with IB, do you have one XIB file or many?

For a popover, I have a single xib, the one for the customView that will be the content of the popover.


Note: when you insert an NSPopover in the storyboard, that creates 2 objects : NSPopoverController and NSPopover

Your viewController must also conform to NSPopoverDelegate


If you use becomeFirstResponder, that means the popover will show even if you tab from one field to this field. This could be disturbing for user as he/she is only tabbing through the different textFields.

Implementing in shouldBeginEditing would call the popover only when user clicks in the textField.

Depends what UI you need.

In Swift 4, XCode 9 this code:


let popoverViewController = storyboard?.instantiateController(withIdentifier: "helpViewController")


returns compiler error, can't convert String to NSStoryboard.SceneIdentifier...


cam someone share the current way to load a NSViewController in Swift 4?

Is it IOS or OSX ?