首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Iphone >

View 关闭有关问题

2012-01-12 
View 关闭问题在一个tableview的选中事件中,我弹出了一个subView- (void)tableView:(UITableView *)tableV

View 关闭问题
在一个tableview的选中事件中,我弹出了一个subView 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

myViewController *ViewController =[[myViewController alloc]initWithNibName:@"myViewController" bundle:nil]; 
//[self.navigationController pushViewController:ViewController animated:YES]; 
[self.view addSubview:ViewController.view]; 

然后在subView中,我执行 
- (IBAction)remove:(id)sender 



[self.view removeFromSuperview]; 



结果是整个app都关闭了,而不是关闭subView,回到tableView所在的view

[解决办法]
你尝试一下
[ViewController.view removeFromSuperview];
[解决办法]
果然是崩溃了。

崩溃的原因是你调用了release没错。

首先,你的理解是错误的。

是你alloc的,你就要负责到底,你要调用release,不会自行释放。

不可以去掉super dalloc(去掉了有一些东西就没释放)

viewcontroller没释放,你点一下,内存泄露一次。

和tableview无关。

addsubview和push不一样,push可以release,因为它内部retain了,addsubview就不可以,因为controller都没了,view也不能用了。

如果不想push,可以使用做一个成员变量来管理,让他来retain,你就可以调用release了。

热点排行