Search code examples
swiftcocoanibappkit

NSView loaded from nib file has disabled controls and actions doesn't work too


I have a hierarchy of views loaded from nib files. Last (deepest) level is like this:

let xValue =  (point as! DynamicPoint).xValue
let xViewController =  DynamicValuePanelViewController(nibName: "DynamicValuePanelView", value: xValue)
xValueView.addSubview(xViewController.view)

OK, it almost works. I can see in NIB NSView values of xValue object like its name, value, some [String] array for NSPopUpButton values. But if I connect NSTextField action to File's Owner it's never fired. If I bind value – getter works, setter – never. I added NSPopupButton, and bind values – yes, they're in menu but they are disabled, action from button never fired. What could cause such a strange one-way behavior? I can get everything, but set nothing.


Solution

  • As @Willeke said — NSViewControllers must have strong reference. Can't create them „on the fly”.

    class SomeController:NSViewController {
    
        var xViewController: DynamicValuePanelViewController!
        @IBOutlet var xValueView: NSView!
    
        func loadView(for xValue: SomeCustomObject) {
             xViewController =  DynamicValuePanelViewController(nibName: "DynamicValuePanelView", value: xValue)
             xValueView.addSubview(xViewController.view)
    
        }
    }