iphone - @synthesize IBOutlet 属性

标签 iphone objective-c ios

我是 Objective-C 新手,正在阅读 Alasdair Allan 的“iPhone 编程”。在阅读时,我发现了这段代码:

@interface RootController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
    UITableView *tableView;
    NSMutableArray *cities;
}
// warning: remember this tableView
@property (nonatomic, retain) IBOutlet UITableView *tableView;

相对的实现是这样开始的:

@implementation RootController
@synthesize tableView;

现在:我了解到 @synthesize 是一种避免无聊的 getter 和 setter 的捷径。

但是我有一些问题:

  1. 在实现 tableView 的代码中从未显式调用但 dealloc 释放了它;
  2. 如果它从未被显式调用,为什么要使用 @synthesize?

IBOutlets 是否必须合成?

最佳答案

来自 Memory Management of Nib Objects ,

When a nib file is loaded and outlets established, the nib-loading mechanism always uses accessor methods if they are present (on both Mac OS X and iOS). Therefore, whichever platform you develop for, you should typically declare outlets using the Objective-C declared properties feature.

For iOS, you should use:

@property (nonatomic, retain) IBOutlet UIUserInterfaceElementClass *anOutlet;

You should then either synthesize the corresponding accessor methods, or implement them according to the declaration, and (in iOS) release the corresponding variable in dealloc.

关于iphone - @synthesize IBOutlet 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6666473/

相关文章:

ios - NSError** 问题在多功能调用中反向传播

ios - iOS应用程序的证章编号是否有限制?

ios - 使用 MagicalRecord 有效保存

iphone - 来自 App Store 上的应用程序的崩溃报告

objective-c - 在 Objective-C 中,如何使变量可以从多个类访问

ios - 从另一个 watch 应用程序中启动 Apple Watch 消息应用程序

ios - UIScrollView setZoomscale 不适用于 ViewWillApper

iphone - 如何使用 plist 项目作为下一个 View 的 self.title

iphone - iOS 6 中不显示启动画面

iPhone应用企业分发流程