How to release a viewcontroller while using addSubview?
I've the code as below:
+(void) addHeader:(UIViewController*) hostViewController requiresBackBtn:(BOOL)BooleanValue
{
ApplicationHeader *appHeader = [[ApplicationHeader alloc] initWithNibName:@"ApplicationHeader" bundle:nil];
appHeader.hostViewController = hostViewController;
[appHeader.view setFrame:CGRectZero];
[hostViewController.view addSubview:appHeader.view];
if (BooleanValue) {
[appHeader.view setFrame:CGRectMake(0, 0, 320, 97)];
}
else {
[appHeader.backBtn setHidden:TRUE];
[appHeader.view setFrame:CGRectMake(0, 0, 320, 74)];
}
// [appHeader release]; // This call tends to app Crash!!!!!开发者_C百科
}
If I call release to appHeader then the app crashes I press the button which found in appHeader!! And if I doesn't this is a memory leak.
What to do now? :(
I don't know what an ApplicationHeader is, but I know that yours has a retained view from
[hostViewController.view addSubview:appHeader.view];
Would ApplicationHeader be better as a subclass of UIView
? Without more information, I'm not sure what to suggest as a solution but I think you need to rethink what you are trying to accomplish.
Adding other controller's view in your viewController is against Apple's "one controller per screen" policy. It will surely give level 1 and 2 memory warnings and app will crash intempestively.
精彩评论