Question

I use the following code to switch between view controllers..(works fine) I have many view controllers too by the way Im not just switching back and forth between 2

NSString * storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];

MyTableViewVC *detailView = (MyTableViewVC *)[storyboard instantiateViewControllerWithIdentifier:@"MyTableViewVC"];

    //pass data through to VC

[self presentViewController:detailView animated:NO completion:nil];

I see the memory use climbing as I transition between view controllers

So i did some research and realized Im not dismissing the previous view controller. I use the following code [self dismissViewControllerAnimated:NO completion:nil]; before I call presentViewcontroller: (I also tried using it after) and it doesn't work. If i use it after nothing happens.. using it before I get the following warning

Thread 1:EXC_BAD_ACCESS(code=1.... blah blah let me know if you need the rest

Ive also tried to do something like this..

[detailView presentViewController:detailView animated:NO completion:nil];
[self dismissViewControllerAnimated:NO completion:nil];

What am I doing wrong?

Was it helpful?

Solution

Below is the code to remove VC from the navigation stack.

NSMutableArray *navigationArray = [[NSMutableArray alloc] initWithArray: self.navigationController.viewControllers];

// [navigationArray removeAllObjects];    // This is just for remove all view controller from navigation stack.
[navigationArray removeObjectAtIndex: 2];  // You can pass your index here
self.navigationController.viewControllers = navigationArray;
[navigationArray release];

However note, by doing this you will have problem to go to previous VC as you are removng the previous VC from the stack.

As you are complaining about memory, I would say DOUBLE CHECK code once again and investigate where memory is getting used more. Incase if that object is not needed, release that object so that memory issue would not be there.

OTHER TIPS

If you want to back to root view controller, you must use this code.

[self.navigationController popToRootViewControllerAnimated:YES];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top