在一个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

解决方案 »

  1.   

    你尝试一下
    [ViewController.view removeFromSuperview];  
      

  2.   

    原因找出了 
    myViewController *ViewController =[[myViewController alloc]initWithNibName:@"myViewController" bundle:nil]; 
    //[self.navigationController pushViewController:ViewController animated:YES]; 
    [self.view addSubview:ViewController.view]; 我在这里最后加了[ViewController release]; 
    根据内存管理方法,因为viewcontroller是alloc分配的,所以要自行释放。 
    但多了这句反而错,是否在subView中执行[self.view removeFromSuperview]; 
    另外在subView中,去掉【super dealloc】也正常运行,但编译器会有一个警告。会同时释放ViewCntroller,还是ViewController其实并没有释放?顺便说下,这段代码是在tableView的某行选中时,激发的。不知是否跟tableView有关。
      

  3.   

    果然是崩溃了。崩溃的原因是你调用了release没错。首先,你的理解是错误的。是你alloc的,你就要负责到底,你要调用release,不会自行释放。不可以去掉super dalloc(去掉了有一些东西就没释放)viewcontroller没释放,你点一下,内存泄露一次。和tableview无关。addsubview和push不一样,push可以release,因为它内部retain了,addsubview就不可以,因为controller都没了,view也不能用了。如果不想push,可以使用做一个成员变量来管理,让他来retain,你就可以调用release了。
      

  4.   

    不解,真的是会泄露么?
    参考:
    http://www.cocoachina.com/bbs/simple/?t30187.html
    http://heidianfeng.blog.163.com/blog/static/618434562010103010817882/
    myViewController *ViewController =[[myViewController alloc]initWithNibName:@"myViewController" bundle:nil];  //ViewController retaincount+1
     
    [self.view addSubview:ViewController.view];  //此处也是会被retain,问题是谁被retain, ViewController?还是ViewController.view?[ViewController release];//从网上找到的多视图的程序,都没这句,但按内存管理规则,是该release
    搞的头都晕了,请高手指教。  
      

  5.   

    按照官方的说法,addsubview,retain的是view。
    http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html
    addSubview:
    Adds a view to the end of the receiver’s list of subviews. - (void)addSubview:(UIView *)view
    Parameters
    view
    The view to be added. This view is retained by the receiver. After being added, this view appears on top of any other subviews. Discussion
    This method retains view and sets the view’s next responder to the receiver, which is its new superview. Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview. addsubview的retain,release不用我们处理。
    问题是,我们alloc了,然后release,难道错了吗?
      

  6.   

    那两个链接操作的都是view好不好。release都是view。你alloc controller,addview(retainview),然后release controller(把view的家都拆了,还让不让活了)基础的概念还没掌握没耐心了。不过我半夜回答,你五点半就起来了,也算比较猛的了。
      

  7.   

    确实,我基础概念还没掌握的很好。学objective-c也就这几天,也没怎么学过C,特别是内存的手动释放很不习惯。
    你的意思我只能大概推测。
    controller有一个view的属性指向view,当contrller被释放时,指向的view便无法找到。所以会造成程序崩溃。按照你的说法,将controller作为成员变量,在@property的时候retain了一次,然后在dealloc中需要release一次。然后controller需要alloc初始化,那么是否也需要在dealloc中再release一次,也就是release两次?
      

  8.   

    另外,我看到网上一种说法,说的[self.view removeFromSuperview];  
    其实只是将view隐藏,并没有释放,那么是否还需要释放view?