Search code examples
objective-ccocoanibnspopover

Should an outlet to a view controller class be weak or stong? OSX app


Here's what i did.

  1. Make a clean OSX project.
  2. went to main.xib and dragged a popover controller. This created 2 visible objects on on interface builder.
  3. I went to the appDelegate.h file and did

    `-@Property (assign) IBOutlet NSViewController *popVC;

  4. Then i went to the applicationDidFinishLaunching: method and did

    popVC = [[NSViewController alloc] init];

Result: I get the following error message:

enter image description here

Shouldnt objects on a nib be weak since it is already owned by the nib?


Solution

  • Outlets to view controllers should be strong. The NIB doesn't own the objects, its just an archive. Outlets to views should usually be weak but that's because the view is retained by its superview (the superview is usually retained by its view controller).


    As an aside, you shouldn't be doing:

    popVC = [[NSViewController alloc] init];
    

    Because popVC is being unarchived, created and set when the NIB is loaded. By creating and setting an instance yourself you're throwing the NIB version away. This applies to all outlets - the purpose of an outlet I'd to be filled in when a NIB is loaded.