Delete last UIViewController after modal segue

So, I have 3 view controllers in the storyboard (VC1, VC2 and VC3).

Each view has a button that calls IBAction, which calls this method to switch to another view:

[self doSegue: myViewController_ID]; -(void) doSegue:(NSString *)_myViewController_ID { //get UiViewController from storybord with Unique ID UIStoryboard *storyboard = self.storyboard; UITableViewController *svc = [storyboard instantiateViewControllerWithIdentifier:_myViewController_ID]; //set presentation & transition styles svc.modalPresentationStyle = UIModalPresentationFullScreen; svc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; //do segue [self presentViewController:svc animated:YES completion:nil]; } 

Lets set that I go from VC1 to VC2, once on VC2

I want to delete the previous ViewController (VC1). and if I now switch to VC3 from VC2, I want to remove the VC2 stack or view from the hierarchy, etc.

This is because I will not offer the method [self dismissViewControllerAnimated:YES completion:nil];

I do not want the memory to grow as a result of the accumulation of the controller in the stack.

NOTE. I will not use the navigation controller or the tab controller, only the view controller.

Thank you for your help.

+4
source share
1 answer

Just guessing, I have not tried ...

Save the VC1 link to VC2 -> Submit using prepareForSegue:

Then on VC2

 [self.previousViewController willMoveToParentViewController:nil]; [self.previousViewController removeFromParentViewController]; 

To make sure you insert your view controller

 - (void)dealloc { NSLog(@"dealloc: %@", self); } 

and look at the console


Edit: Instead of removing each view controller after segue, you can do this when you get a memory warning. You can also try using shutViewControllerAnimated: termination: after segue

 - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. // ??? if ([self isViewLoaded] && self.view.window == nil) { NSLog(@"UNLOADING"); self.view = nil; [self dismissViewControllerAnimated:NO completion:nil]; } } 
+4
source

Source: https://habr.com/ru/post/1439487/


All Articles