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 userInfo];
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.