Desmond
Desmond

Reputation: 5011

how to clear memory of previous ViewController

I encountered this problem when doing my MCQ Game app testing.

I'm using ARC & Storyboard custom segue and [self performSegueWithIdentifier:@"VC" sender:self]; to push from ViewController1 to ViewController2 but very seldom popping back the ViewController1 due to the naturals of the app, moving forward from question to question. the app will become very very sluggish after multiple times pushing. what is the best way to solve the problem?

Custom segue:

-(void)perform {
    UIViewController *sourceVC = (UIViewController *) self.sourceViewController;
    UIViewController *destinationVC = (UIViewController *) self.destinationViewController;

    [UIView transitionWithView:sourceVC.navigationController.view duration:0.5
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                        [sourceVC.navigationController pushViewController:destinationVC animated:NO];
                                           }
                    completion:^(BOOL  completed)
                    {     

                        //[sourceVC.navigationController pushViewController:destinationVC animated:NO];
                    }                      
                    ];



}

Upvotes: 0

Views: 3639

Answers (2)

Desmond
Desmond

Reputation: 5011

i changed the custom segue from push to pop, based on my view controller name, and it works :)

-(void)perform {
    UIViewController *sourceVC = (UIViewController *) self.sourceViewController;
    //UIViewController *destinationVC = (UIViewController *) self.destinationViewController;

    NSInteger index = -1;
    NSArray* arr = [[NSArray alloc] initWithArray:sourceVC.navigationController.viewControllers];
    for(int i=0 ; i<[arr count] ; i++)
    {
        if([[arr objectAtIndex:i] isKindOfClass:NSClassFromString(@"mainViewController")])
        {
            index = i;
        }       
    }    

    [UIView transitionWithView:sourceVC.navigationController.view duration:0.5
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{

                        [sourceVC.navigationController popToViewController:[arr objectAtIndex:index] animated:NO];

                    }
                    completion:^(BOOL  completed)
     {     

         //[sourceVC.navigationController pushViewController:destinationVC animated:NO];
     }                      
     ];



}

Upvotes: 1

Oscar Gomez
Oscar Gomez

Reputation: 18488

I suspect there is a strong reference cycle going on. Run Leaks to make sure that the previous view controller does not have a strong reference after you push the new one.

Upvotes: 1

Related Questions