childViewController not getting deallocated after removing from parentViewController - uiview

I'm adding a child view controller as follows:
loginViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
loginViewController.delegate = self;
[self addChildViewController:loginViewController];
[self.view addSubview:loginViewController.view];
loginViewController.view.frame = self.view.bounds;
[loginViewController didMoveToParentViewController:self];
And removing it as follows:
[loginViewController willMoveToParentViewController:nil];
[loginViewController.view removeFromSuperview];
[loginViewController removeFromParentViewController];
If I understand correctly that should trigger dealloc in LoginViewController but it does not ? Am I missing something?

Not quite. Removing the controller's view has nothing to do with the controller being released, just the controller's view (which is retained by the controller, so it's not deallocated.
You have [self addChildViewController:loginViewController];, are you storing it somewhere? perhaps an array? If that's the case, there's your leak. You need to remove it from wherever you put it

Related

how to remove child class from parent when I get sucess message in the childview controller

I am presenting viewcontroller as chid viewcontroller.I Am presenting child viewcontroller in a popupview which is in parent class.plase find the below image to get clear idea.
This the code i used to add present childvc in parent.
[self.view addSubview:yopopup_view];
yopopup_view.hidden=NO;
yoviewcontroller = [storyBoard instantiateViewControllerWithIdentifier:#"yoviewcontroller"];
CGRect contentFrame = yocontantview.frame;
contentFrame.origin.y = yocontantview.frame.origin.y;
contentFrame.size.height = yocontantview.frame.size.height;
yocontantview.frame = contentFrame;
yoviewcontroller.view.frame = yocontantview.bounds;
[yocontantview addSubview:yoviewcontroller.view];
[self addChildViewController:yoviewcontroller];
[yoviewcontroller didMoveToParentViewController:self];
Now i the childvc I have an api class when I get success message from backend I have remove child class from parentvc.
I am using the below code to remove child class from parent class.
[self willMoveToParentViewController:nil];
[self.view removeFromSuperview];
[_delegate closeyopopmethod:#"close"];
[self removeFromParentViewController];
When I use the code I am removing the child class but I have to remove popupview from the parent class superview.How to do that.
Thanks for quick responce.
You haven't set the delegate of yoviewcontroller, so set it to self when you initialized it.
yoviewcontroller = [storyBoard instantiateViewControllerWithIdentifier:#"yoviewcontroller"];
//Set the delegate to self
yoviewcontroller.delegate = self;
//Your other code
UIViewController *vc = [self.childViewControllers lastObject];
[vc.view removeFromSuperview];
[vc removeFromParentViewController];

How to Remove Last Added View Controller From Container

How to Remove Last Added View Controller From Container
I have used this code to remove last added view container
UIViewController *vc = [self.childViewControllers lastObject];
[vc.view removeFromSuperview];
[vc removeFromParentViewController];
[self.ctr addSubview:news.view];
First Call Method willMoveToParentViewController:nil
Then Call Remove From Super View
[vc willMoveToParentViewController:nil];
[vc.view removeFromSuperview];

Why is my initWithNibName method returning a nil view?

I'm calling this method when trying to present a new view controller. The commented out lines are the other things I've tried with no success.
-(void)presentMailBoxViewController{
TPMailBoxViewController* mailBoxViewController=[[TPMailBoxViewController alloc]initWithNibName:#"TPMailBoxViewController" bundle:nil];
[self.navigationController pushViewController:mailBoxViewController animated:YES];
//[self.navigationController presentViewController:mailBoxViewController animated:YES completion:NULL];
//[self presentViewController:mailBoxViewController animated:YES completion:NULL];
}
I've set the file owner as my corresponding view controller.
I've even linked the view object of my xib into my view controller as a property.
But, when I break on [self.navigationController pushViewController:mailBoxViewController animated:YES];
The view property of the view controller is nil. What gives?
"UIViewController" already has a "view" property, so you don't need "myView".
Open up your storyboard or XIB file and make certain that the "view" that you want to be displayed with your is connected in your storyboard or XIB file to your TPMailBoxViewController object.
Right after you "initWithNibName", the view controller object exists but its "view" property is likely to be nil because the view has not been loaded yet.
It's when you push the view controller, that is when your "TPMailBoxViewController" will get the "viewWillLoad" and "viewWillAppear" methods being called and when the view is loaded, the "view" outlet will change from nil to something real.
A project clean and this one line change in my presentMailBoxViewController fixed my issue. Still would like to know why though. Thanks everyone!
-(void)presentMailBoxViewController{
TPMailBoxViewController* mailBoxViewController=[[TPMailBoxViewController alloc]initWithNibName:#"TPMailBoxViewController" bundle:nil];
[self presentViewController:mailBoxViewController animated:YES completion:NULL];
}
In your AppDelegate, you need to set the root object of the window as the navigation controller. You can do it like this:-
YourViewController *rootObj = [[LoginViewController alloc] init];
UINavigationController *navObj = [[UINavigationController alloc] initWithRootViewController:rootObj];
[self.window setRootViewController:navObj];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
After this, try pushing the viewcontroller, it should work

Issue with definesPresentationContext / UIModalPresentationCurrentContext - Current context view controller gets lost

This only accours if you are presenting in a view controller that is managed by a navigation controller.
The reproduction steps are:
1 - Present a view controller using UIModalPresentationCurrentContext
self.definesPresentationContext = YES;
ViewController* viewController = [[ViewController alloc] init];
viewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[presentOnViewController presentViewController:viewController animated:YES completion:nil];
2 - Present a view controller over the top using the default full screen presentation style
ViewController* viewController = [[ViewController alloc] init];
[self presentViewController:viewController animated:YES completion:nil];
3 - Dismiss the top presented view controller (the full screen one)
[self dismissViewControllerAnimated:YES completion:nil];
Now the problem is the 2nd view controller (presented using UIModalPresentationCurrentContext) disappears. Also it is impossible to present another view controller using UIModalPresentationCurrentContext, because the system thinks its still there.
I believe the issue is a bug in the framework. As mentioned it only occurs when the presenting in a view controller managed by a navigation controller. There is a nasty work around which uses the containment API. It creates a dummy view controller which views are presented from. The steps are:
1 - When presenting a view in context who's parent is a navigation controller, use a dummy view controller:
- (void)presentInContext
{
UIViewController* presentOnViewController = self;
if ([self.parentViewController isKindOfClass:[UINavigationController class]])
{
// Work around - Create an invisible view controller
presentOnViewController = [[DummyViewController alloc] init];
presentOnViewController.view.frame = self.view.frame;
// Containment API
[self addChildViewController:presentOnViewController];
[self.view addSubview:presentOnViewController.view];
[presentOnViewController didMoveToParentViewController:self];
presentOnViewController.definesPresentationContext = YES;
}
ViewController* viewController = [[ViewController alloc] init];
viewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[presentOnViewController presentViewController:viewController animated:YES completion:nil];
}
2 - When dismissing the view controller tidy up
- (void)dismissSelf
{
__weak UIViewController* presentingViewController = self.presentingViewController;
[self dismissViewControllerAnimated:YES completion:^{
// Remove the dummy view controller
if ([presentingViewController isKindOfClass:[DummyViewController class]])
{
[presentingViewController willMoveToParentViewController:nil];
[presentingViewController.view removeFromSuperview];
[presentingViewController removeFromParentViewController];
}
}];
}
Thats it... The fix is dirty, but does the trick with no visual flicker.

The [super dealloc] in dealloc of UIViewController giving problem in iPad

I am working on an application where i am pushing one view controller on to a UINavigationController and releasing it immediately as the navigation controller retains it.When i am poping the view controller the dealloc method is being called as expected but the problem is the app is getting crashed.If i observe in GDB by enabling NSZombie its saying that -[MyViewController isKindOfClass:]: message sent to deallocated instance 0x6847a00.If i remove [super dealloc]from my view controller's deallocmethod its working just fine.I have nothing else in dealloc method except [super dealloc].What could be the problem here, can any one please help.The code snippet is below:
MyViewController *myViewController = [[MyViewController alloc] initWithNibName:nil bundle:nil];
myViewController.path = selectedPath; //very important to set path here
myViewController.parentViewController = self;
[self cleanBookshelf];
[self.navigationController pushViewController:myViewController animated:NO];
[myViewController release];
[indicatorView removeFromSuperview];
[loadingindicator stopAnimating];
and i am poping in one action method of myViewController as
-(IBAction)goBack:(UIButton*)sender{
[self.navigationController popViewControllerAnimated:YES];
}
Just guessing, but I suspect that the problem is with this line:
myViewController.parentViewController = self;
UIViewController's parentViewController property is marked readonly, and I'd take that as a strong message that you shouldn't mess with it.

Resources