在一个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
//[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];
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有关。
参考:
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
搞的头都晕了,请高手指教。
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,难道错了吗?
你的意思我只能大概推测。
controller有一个view的属性指向view,当contrller被释放时,指向的view便无法找到。所以会造成程序崩溃。按照你的说法,将controller作为成员变量,在@property的时候retain了一次,然后在dealloc中需要release一次。然后controller需要alloc初始化,那么是否也需要在dealloc中再release一次,也就是release两次?
其实只是将view隐藏,并没有释放,那么是否还需要释放view?