我是 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 的捷径。
但是我有一些问题:
- 在实现 tableView 的代码中从未显式调用但 dealloc 释放了它;
- 如果它从未被显式调用,为什么要使用 @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/