Fabio
Fabio

Reputation: 1973

Navigate in uiviewcontroller iOS

enter image description here

I have a problem in memory management, when navigate to specific UIViewController

Example:

I have 3 UIViewController and use Storyboard modal segue and I stay in the first and I need go to the number 3 directly

I use this code works fine, but when i need return to the 1 and I if repeat this code. I receive a memory warning and crash later.

This is my code:

go to view 3 ->

UIStoryboard *storybord = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController * viewTree = [storybord instantiateViewControllerWithIdentifier:@"Three"];
[self presentViewController:viewTree animated:YES completion:nil];

go to view 1 ->

UIStoryboard *storybord = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController * viewOne = [storybord instantiateViewControllerWithIdentifier:@"One"];
[self presentViewController:viewOne animated:YES completion:nil];

Upvotes: 0

Views: 70

Answers (1)

Stephen Fox
Stephen Fox

Reputation: 14470

You must be constantly be presenting each view controller over each other, which is raising the memory warning issue. To present ViewControllerThree use the following code in ViewControllerOne

@implementation ViewControllerOne

- (IBAction) goto3 {

      UIStoryboard *storybord = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
      UIViewController * viewTree = [storybord instantiateViewControllerWithIdentifier:@"Three"];
      // Brings you to the third view controller.
      [self presentViewController:viewTree animated:YES completion:nil];
}

@end

And then to go back to ViewControllerOne implement this code in ViewControllerThree

@implementation ViewControllerThree

-(IBAction) backTo1 {
    // Dismisses the third view and brings you back to the first view controller.
    [self dismissViewControllerAnimated:YES completion:nil];
}

Upvotes: 1

Related Questions