我目前正在研究 Apress 的“开始 iPhone 3 开发”。他们在示例应用程序中使用的标准类似于以下代码:
- (void)viewDidLoad {
BlueViewController *blueController = [[BlueViewController alloc]
initWithNibName:@"BlueView" bundle:nil];
self.blueViewController = blueController;
[self.view insertSubview:blueController.view atIndex:0];
[blueController release];
}
8.14.11 UPDATE(附加信息)
blueViewController 声明如下:
@property (retain, nonatomic) BlueViewController *blueViewController;
每当他们执行时,alloc
他们将其放入某个临时变量(此处blueController
)中,然后分配它,然后释放它。这个临时变量对我来说似乎是多余的。
我将代码简化如下:
- (void)viewDidLoad {
self.blueViewController = [[BlueViewController alloc]
initWithNibName:@"BlueView" bundle:nil];
[self.view insertSubview:blueViewController.view atIndex:0];
}
- (void)dealloc {
[blueViewController release];
[super dealloc];
}
我修改后的代码在 iPhone 模拟器中运行相同。现在,我知道了如果你分配了一些东西就需要释放它的规则。我在我的dealloc
方法中涵盖了这一点。但是直接在(被调用ViewDidLoad
的函数)中发布有什么好处吗?alloc
或者release
在你的dealloc
方法中有这样的方法同样可以吗?
感谢您的帮助,
-j