Search code examples
iphonestoryboardsegueviewdidload

Is there a way to avoid viewDidLoad beeing called after every segue?


i initialize tables, data etc in my main ViewController. For more settings, i want to call another Viewcontroller with:

 DateChangeController *theController = [self.storyboard instantiateViewControllerWithIdentifier:@"dateChangeController"];
[self presentViewController:theController animated:YES completion:^{NSLog(@"View controller presented.");}];

And some clicks later i return with a segue (custom:)

NSLog(@"Scroll to Ticket");
[self.sourceViewController presentModalViewController:self.destinationViewController animated:YES];

My Problem:

After returning to my main ViewController, viewDidLoad is called (everytime).I read, that the ARC releasing my mainView after "going" to the other ViewController and calling the viewDidUnload Method, but i want to keep all my data and tables i initialize at the beginning..

Any solution? Thank you very much!


Solution

  • The problem is that you are doing this:

    main view controller ->
        date change controller ->
            a *different* main view controller
    

    In other words, although in your verbal description you use the words "returning to my main ViewController", you are not in fact returning; you are moving forward to yet another instance of this main view controller every time, piling up all these view controllers on top of one another.

    The way to return to an existing view controller is not to make a segue but to return! The return from presentViewController is dismissViewController. You do not use a segue for that; you just call dismissViewController. (Okay, in iOS 6 you can in fact use a segue, but it is a very special and rather complicated kind of segue called an Unwind or Exit segue.)

    If you do that, you'll be back at your old view controller, which was sitting there all along waiting for your return, and viewDidLoad will not be called.

    So, this was a good question for you to ask, because the double call of viewDidLoad was a sign to you that your architecture was all wrong.