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

点点圈 提交于 2019-12-06 08:47:14

问题


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:

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


回答1:


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.



来源:https://stackoverflow.com/questions/17256056/should-an-outlet-to-a-view-controller-class-be-weak-or-stong-osx-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!