ios - 为什么 Xcode 会为 IBOutlet 创建弱引用?

标签 ios objective-c automatic-ref-counting iboutlet

<分区>

我注意到,当我在 Storyboard中创建导出时,它会生成以下代码 __weak IBOutlet UILabel *mLabel;

为什么声明它是一个弱指针?据我了解,当对象被释放时,它的所有成员也将被释放。在我的大部分代码中,我都将导出声明为强指针。这会产生问题吗?

最佳答案

根据 iOS 开发者库:link here

部分:管理 Nib 文件中对象的生命周期

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).

@property(弱)IBOutlet MyView *viewContainerSubview;

@property (strong) IBOutlet MyOtherClass *topLevelObject;

关于ios - 为什么 Xcode 会为 IBOutlet 创建弱引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21654113/

上一篇:ios - MarqueeLabel 不工作,它只是在屏幕上显示为 UILabel

下一篇:ios - 从 SKScene 呈现 UIViewController

相关文章:

ios - DefinesPresentationContext/UIModalPresentationCurrentContext 的问题 - 当前上下文 View Controller 丢失

ios - 相机卡在 Google Cardboard + Unity 应用程序中重新加载

ios - 遍历数组并在 Swift 中增加计数器

objective-c - UITableViewCell 错误中的 UICollectionViewCell 中的 UIImageView

objective-c - OCMock 随机崩溃

ios - 旋转后 UITapGestureRecognizer 不触发

objective-c - "Initializer element is not a compile-time constant"为什么?

objective-c - 在 ARC 下,如何释放 NSArray 中的元素?

iphone - [CFNumber 发布] : message sent to deallocated instance

iphone - 在 iOS 4 上运行 iOS 5 应用程序有困难