addsubView释放不正常工作

创建一个类,添加到当前视图中,然后释放:

  TestViewController *tView=[[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];
tView.view.frame=CGRectMake(10, 10,tView.view.frame.size.width , tView.view.frame.size.height);
[self.view addSubview:tView.view];
[tView release];

添加按钮到TestViewController,然后按按钮时,就崩溃了。控制台的信息:

-[TestViewController performSelector:withObject:withObject:]: message sent to deallocated instance

调用[tView release]时,TestViewController的dealloc方法会自动调用。然后这个类的object都会被释放。可能是因为你释放了dealloc中的button,引起应用崩溃。

没有特别好的解决方法,你可以创建一个自定义视图,然后将原来添加到viewcontroller的view添加到self.view中。