2012-02-23 80 views
1

我正在努力研究如何正確設置我的視圖控制器以正常處理內存警告。iOS內存警告tableView EXC_BAD_ACCESS在多視圖應用程序中崩潰

目前,每當應用程序收到內存警告時,我都會從導航控制器堆棧中的視圖中收到EXC_BAD_ACCESS崩潰。

我的表格視圖出現訪問不良的情況。下面是我如何實例吧:

- (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文件,所以我應該在其他地方構建它?我不知何故錯誤地將它傳遞給視圖控制器? etc等

任何幫助將不勝感激。我還沒有注意到內存警告時發生的事件序列,而1級內存警告似乎是令人厭惡的。

回答

2

您在table上打電話release兩次;一旦推遲發佈時autorelease當您創建它時,並且在您添加它作爲self.view的子視圖後再次使用[table release];發佈。請記住,如果tableView的屬性爲'retain',那麼它將在分配時保留(當使用點語法分配時) - 並且addSubview在添加它時也會保留table。所以,你只需要留在autorelease那裏 - 因爲這延遲釋放(其將被保留,當你說self.tableView = table;出現這種情況

+0

是啊,我看到了當我發佈這個問題時,'autorelease',lol。*打了額頭* - 謝謝,先生。 – Murdock 2012-02-23 02:42:02

+0

另外,我推薦使用UITableViewController - 它爲你做了很多的UITableView的alloc等等 – 2012-02-23 02:46:49

+0

是的,我習慣使用'UITableViewController'作爲一種方便,不幸的是我們現在需要在我們的一些屏幕上做足夠的視圖定製,我需要更細粒度的控制實例化ta我自己的看法。再次感謝! – Murdock 2012-02-23 02:49:02

2

試試這個被抵消。

- (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]; You have already release table with autorelease. 

    ...other stuff... 
} 
+0

謝謝@fannheyward。你是對的。我只想以響應速度去接受我在這裏接受的答案。 – Murdock 2012-02-23 03:04:01