视图控制器类的出口应该是弱电还是强电? OSX应用 [英] Should an outlet to a view controller class be weak or stong? OSX app

查看:97
本文介绍了视图控制器类的出口应该是弱电还是强电? OSX应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我所做的.

  1. 制作一个干净的OSX项目.
  2. 转到main.xib并拖动一个弹出框控制器.这在界面生成器上创建了2个可见对象.
  3. 我去了appDelegate.h文件并做了

  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(分配)IBOutlet NSViewController * popVC;

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

然后我去applicationDidFinishLaunching:方法并做了

popVC = [[NSViewController alloc] init];

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?

推荐答案

用于查看控制器的插座应为strong. NIB不拥有对象,而只是一个存档.视图的出口通常应该为weak,但这是因为视图由其超级视图保留(超级视图通常由其视图控制器保留).

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];

因为正在加载NIB时正在归档,创建和设置popVC.通过自己创建和设置实例,您将放弃NIB版本.这适用于所有插座-加载NIB时要填写的插座的目的.

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.

这篇关于视图控制器类的出口应该是弱电还是强电? OSX应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆