@synthesize IBOutlet属性

8 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;
Run Code Online (Sandbox Code Playgroud)

相对实现以这种方式开始:

@implementation RootController
@synthesize tableView;
Run Code Online (Sandbox Code Playgroud)

现在:我了解到@synthesize是一种避免无聊的吸气剂和制定者的捷径.

但我有一些问题:

  1. 在实现的代码中,tableView 永远不会被显式调用,但是dealloc会释放它;
  2. 如果它永远不会被明确调用为什么@synthesize?

是否必须合成IBOutlets?

tas*_*oor 7

Nib对象的内存管理,

当加载nib文件并建立出口时,nib加载机制总是使用存取方法(如果它们存在)(在Mac OS X和iOS上).因此,无论您开发哪个平台,通常都应使用Objective-C声明的属性功能声明出口.

对于iOS,您应该使用:

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

然后,您应该合成相应的访问器方法,或者根据声明实现它们,并且(在iOS中)在dealloc中释放相应的变量.