Memory Managment In iOS StoryBoards

Hello Dear friends!

In This tutorial I want to teach you how to manage  memory in the iOS storyboard.

One of the issues that is always required in high-level programming is the correct memory management.

One of the most important section in iOS Development  is working with storyboards.

When your app is very heavy in graphic design, we must control  migration between different View controllers to avoid memory leaks.

When Developer Use Default Segue between View Controllers and user go from VC1 to VC2 and back from VC2 toVC1 application create new instance from VC1 and in this time we have 2 instance from VC1 in memory.In this situation memory leak occur.And if you check your application resource monitor in xcode you can see very high usage of memory when you exchange between VCs.

My Solution for this issue using navigation view controller and using push segue programmatically.when user press button for segue between VCs , we must dismiss Current View Controller and then segue to another View Controller.wehn we using this Solution memory release current view controller from memory and create new instance from next view controller and put it in the memory and wehn you back to Previous view controller this routine continues.

Now i want to show source code for this Solution.

when user button in VC1 you must implement this code in IBAction,dont forget that import VC2 in VC1

and in the storyboard you must set StoryBoard ID for View Controller such as below image:

        UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        ViewController2 *ViewController2 = [storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];
        UINavigationController *navController = self.navigationController;
        NSMutableArray *controllers=[[NSMutableArray alloc] initWithArray:navController.viewControllers] ;
        [controllers removeAllObjects];
        [navController setViewControllers:controllers];
        [navController pushViewController:ViewController2 animated:NO];

in above source code in the first you get array of view controllers in the application and then remove all of them from view controllers stack and push new view controller.An important point in this Solution is that when you using timer in View Controller you must invalidate timer then using this Solution because when timer is active operation system dont allow dismiss Current View Controller And memory leak occurs.

Sorry for poor English.

good luck;

Add a Comment

Your email address will not be published. Required fields are marked *