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];
Related
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];
I have written a custom view controller container that can replace the existing view controller (VC1) with a new view controller(VC2) i.e. a replace type segue rather than pop/push.
Everything worked fine until testing with iOS 8. It seems a second party I'm using causes a crash if the view controller is replaced before the previous view controller is dealloc
i.e. if [VC2 viewDidLoad] called before [VC1 dealloc].
It is my understanding that I have no control over the dealloc so this kind of behaviour is out of my control. However, I want to make sure that I am not causing this behaviour in the way I'm controlling my child view controllers.
The following is pseudo code for my view controller container:
- (void)replaceStackWithNewController:(UIViewController*)newVC {
for(UIViewController *vc in [self viewControllers]) {
[vc willMoveToParentViewController:nil];
if([vc isViewLoaded]
[[vc view] removeFromSuperview];
}
[vc removeFromParentViewController];
}
//self.viewControllers is my navigation stack
self.viewControllers = nil;
self.childVC = nil;
if(newVC != nil) {
self.childVC = newVC;
}
[self addChildViewController:newVC];
//this adds the view to a pre existing wrapper
[self addView:newVC.view ToWrapper:wrapper];
//add controller to stack
[self.viewControllers addObject:newVC];
[newVC didMoveToParentViewController:self];
}
Tonight one faced with this:
- (void)loadView {
VC *vc = [[VC alloc] initWithStyle:UITableViewStyleGrouped];
[self addChildViewController:vc];
[vc removeFromParentViewController];
[self setView:vc.view];
}
and got a bomb:
uncaught exception 'UIViewControllerHierarchyInconsistency', reason:
'A view can only be associated with at most one view controller at a time!
Q: How we can directly add vc and all its functionality to current ViewController without AddSubView?
Add your table view controller's view as a subview of the current view. You shouldn't call removeFromParentViewController on vc because that would essentially negate the line before. It's also a good idea to set the frame to match the parent.
- (void)loadView
{
[super loadView]; // this will create a basic view
VC *vc = [[VC alloc] initWithStyle:UITableViewStyleGrouped];
[self addChildViewController:vc];
vc.view.frame = self.view.bounds;
vc.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self.view addSubview:vc.view];
[vc didMoveToParentViewController:self];
}
In my program I have UIViewController which has two buttons, one is "signIN" and other is "register". When "Sign IN" button is pressed, it should go to next viewController without using segue and when "register" button is pressed, it should move to next UIViewController by using segue.
Depending on the way you wish to present your view controller you should choose between:
Pushing the new view controller onto the current Navigation controller
[self.navigationController pushViewController:someOtherViewController animated:YES];
Present the new view controller modally
[self presentModalViewController:someOtherViewController animated:YES completion:nil];
Manually adding the new view controller onto the current view controller:
[self addChildViewController:childController];
childController.view.frame = view.bounds;
[view addSubview:childController.view];
[childController didMoveToParentViewController:self];
// To remove:
[self willMoveToParentViewController:nil];
[self.view removeFromSuperview];
[self removeFromParentViewController];
Use pushViewController:
- (void)didPressButton:(UIButton *)sender
{
[self.navigationController pushViewController:nextViewController animated:YES];
}
[[self navigationController] pushViewController:yourPushedViewController animated:YES];
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