我正在努力弄清楚如何正确设置我的 View Controller 以优雅地处理内存警告。
目前,每当应用程序收到内存警告时,我都会从导航 Controller 堆栈中更靠后的 View 中收到 EXC_BAD_ACCESS 崩溃。
我的表格 View 发生了错误的访问。这是我实例化它的方式:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UITableView *table = [[[UITableView alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.size.width, self.view.bounds.size.height - self.navigationController.navigationBar.bounds.size.height) style:UITableViewStyleGrouped] autorelease];
table.dataSource = self;
table.delegate = self;
self.tableView = table;
[self.view addSubview:table];
[table release];
...other stuff...
}
这是我的 viewDidUnload:
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.tableView = nil;
}
出现内存警告时,按预期调用 viewDidUnload,但在
self.tableView = nil
上出现 EXC_BAD_ACCESS 崩溃线。我在错误的地方设置了我的 tableView 吗?我没有使用 nib 文件,所以我应该在其他地方构建它吗?我是否以某种方式错误地将其传递给 View Controller ?等等等等
任何帮助将非常感激。我仍然没有理解内存警告时发生的事件顺序,而 1 级内存警告似乎非常常见。
最佳答案
您调用release
两次 table
;一次与 autorelease
的延迟发布当您创建它时,再次使用 [table release];
将其添加为 self.view
的 subview 后.请记住,如果 tableView
的属性是'retain',那么它将在分配时保留(当使用点语法分配时)——并且,addSubview 将保留table
添加时也是如此。所以,你只需要离开 autorelease
在那里——因为延迟发布(这将被你说 self.tableView = table;
时发生的保留所抵消。
关于iphone - iOS内存警告tableView EXC_BAD_ACCESS在多 View 应用程序中崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9406369/