【问题标题】:Releasing Outlets in a NavigationController在 NavigationController 中释放 Outlets
【发布时间】:2011-12-22 15:56:31
【问题描述】:
我在 NavigationController 的一个视图中确实有 24 个 IBOutlet(没有一个作为属性保留)。有没有好办法释放这些?我感觉它们会导致速度较慢的 3G 和 3GS 设备出现内存问题。
无弧
有什么想法吗?
【问题讨论】:
标签:
iphone
memory-management
uinavigationcontroller
iboutlet
【解决方案1】:
当您将 UIViewControllers 推送到 UINavigationController 时,可能会卸载已“推送”到的 UIViewControllers 的视图以节省内存(因为它们是不可见的)。然后在必要时重新加载视图(并获得 viewDidLoad 回调)。您应该有 IBOutlets 到 UIViewControllers 而不是 UINavigationController,以便它们可以在 viewDidUnload 上释放。释放它们的常用方法是将它们声明为保留属性并在 viewDidUnload 和 dealloc 中将它们设置为 nil(使用 self.outlet 访问器)。或者只使用 ARC。
【解决方案2】:
ViewController.h
@interface ViewController : UIViewController
{
}
@property (retain, nonatomic) IBOutlet UILabel *myLabel;
@end
ViewController.m
#import "ViewController.h"
@implementation ViewController
@synthesize myLabel;
#pragma mark - View lifecycle
- (void)viewDidUnload
{
[self setMyLabel:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[myLabel release];
[super dealloc];
}
@end