4
votes

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

Each view has a button that calls an IBAction, which calls this method to go modal 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 at VC2

What I want is to remove previous ViewController (VC1). and if I now go to VC3 from VC2 I want remove from view hierarchy or stack VC2 and so on.

This is since I will not be providing a [self dismissViewControllerAnimated:YES completion:nil]; method

I don't want the memory to be growing as result of all the view controller accumulation in the stack.

NOTE: I will not be using a navigation controller or tab controller , just the view controller.

Thanks for your help.

1
i don't think it is possible without a navigation controller, are you sure you don't want to use it??Ezeki
It sounds to me like you actually WANT a navigation controller, even if you don't realize it yet. Modal view are intended to interrupt the flow of the current navigation. Like "Go to page 2" --- "not authenticated yet" ----"present modal login screen" --- "authenticated"---"dismiss modal view"---"continue to page 2"mkral
also, if you're not using a UINavigationController because you don't want the topBar to be visible, you can easily get rid of it.mkral
so if I use navigation controller... what I can do to remove the las view controller is to use something to pop the last view like "popToRootViewControllerAnimated:"?Sinuhe Huidobro

1 Answers

4
votes

Just a guess, I didn' tried...

Keep a reference of VC1 on VC2 -> Send it using prepareForSegue:

Then on VC2

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

Just to be sure insert on your View Controller

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

and have a look at the console


Edit: Instead of removing each view controller after the segue, you can do it when you receive a memory warning. You can also try with dismissViewControllerAnimated:completion: after the 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];
    }
}