Search code examples
objective-ccocoainterface-buildernspopover

How to properly set up NSPopover with separate viewController


I have created an extremely simple test program. It has one button. Clicking the button brings up an NSPopover with a label. That label is on a separate xib file with its own NSViewController.

The Goal is to, when on the main screen, when I click the button, a popover will show the xib file of the viewcontroller. And the label of the xib file should have it's text set to "It works".

Well.. It works, but only on after the second loading of the popover. On the first click of the button, the label still has its old default value. But from the 2nd click and onwards, "It works". Does any one have an idea what can be causing this issue? Its only about 5 lines of code The code can be seen on this repository --> https://github.com/patchthecode/testtest


Solution

  • Call [mainScreenPopoverViewController view]; in - (void)windowDidLoad method. This will load your view into memory.

    Before call [mainScreenPopoverViewController view]; (textfield address is 0x0)

    enter image description here

    You should not use strong property for all IBOutlet.

    @property (nonatomic, strong) IBOutlet NSTextField *textField;
    

    Take a look at Resource Programming Guide

    From a practical perspective, in iOS and OS X outlets should be defined as declared properties. Outlets should generally be weak, except for those from File’s Owner to top-level objects in a nib file (or, in iOS, a storyboard scene) which should be strong. Outlets that you create should therefore typically be weak, because:

    • Outlets that you create to subviews of a view controller’s view or a window controller’s window, for example, are arbitrary references between objects that do not imply ownership.

    • The strong outlets are frequently specified by framework classes (for example, UIViewController’s view outlet, or NSWindowController’s window outlet).