Can't release ViewController in function
Why can't I use [y release]
at the end of function?
- (void)functionused:(NSTimer *)timer
{
[password resignFirstResponder];
mainViewController* y=[[mainViewController alloc]initWithNibName:@"mainViewController" bundle:nil];
UIView *currentView = (UIView *)[timer use开发者_JS百科rInfo];
for ( UIView *subview in [self.view.superview subviews] ) {
if ((subview.tag != 14)) {
[subview removeFromSuperview];
}
}
[currentView.superview addSubview:[y view]];
//[y release];
}
Because that will bring the reference count of mainViewController to zero and it will be deallocated. You need to keep a reference to it around somewhere.
The line [currentView.superview addSubview:[y view]];
only adds it's view to the superview. The view will be retained, but the controller itself won't.
In this case, you need to understand the difference between a UIView and a UIViewController. The UIViewController contains a pointer to the UIView instance, but the UIView does not contain a direct reference to the UIViewController. So - when you add the view controller's view to the currentView's superview, it's retain count increments. However, this does not increment the retain count for the view controller. It would go to 0 if released and this would cause the view controller to be deallocated.
精彩评论