Ios – Segues and viewDidLoad/viewDidUnload method

iosmemory-managementuistoryboarduistoryboardsegue

I am making simple storyboard application which has 2 UIViewControllers and I am switching between them by using modal segue. Each UIViewController has one UIButton which is used to perform segue to another UIViewController. In viewDidLoad method I animate appearance of that UIButton on each UIViewController. I am using Cross Dissolve Modal segue.

When I press UIButton on 1st UIViewController I navigate to second UIViewController and animation is executed and 2nd UIViewController is shown. After I press UIButton on 2nd UIView Controller, first UIViewController is shown and it's animation is executed. Everything looks fine and viewDidLoad methods are called for each UIViewController when ever I navigate to it. And that's great.

I tried now to change Modal segue type from Cross Dissolve to other two by default offered in XCode Interface Builder. I changed to Cover Vertical, and everything worked just fine, without changes. But when I tried Flip Horizontal Modal segue, I saw a problem.

When performing Flip Horizontal Modal segue, my UIButton on both UIViewControllers is shown, but animation isn't executed. I tried debugging and I am sure that animation commands are being executed, but animation isn't shown.

So that's my first question: Does anyone know is there any difference between these types of Modal segues which may cause my animation not showing up?

Other questions are related to basic theory of segues and memory management. When I perform segue and navigate to some UIViewController, viewDidLoad method is called every time. So, does that mean I created new object instance each time viewDidLoad method was executed?

I also notice that viewDidUnload method is never called. So, if answer to previous question is affirmative (each viewDidLoad execution creates new object instance), does that mean that my UIViewController object instances are never being unloaded and deleted? Or ARC is doing garbage collection behind the scenes?

If someone could explain how things works with storyboard segues and memory management/object lifecycle and why viewDidUnload method is never being called, I'd be very grateful.


[edit #1: Trying to unload UIViewController after performing modal segue]

[update #1: This shouldn't be done, viewDidUnload will be called automatically]

I am making segue in IBAction attached to UIButton click. I have written this peace of code to perform modal segue.

@try
{
    [self performSegueWithIdentifier:segueToPerform sender:self];
}
@catch (NSException *exception) 
{
    NSLog(@"Exception: %@", exception);
}
@finally
{
    [self viewDidUnload];
}

I have manually called viewDidUnload in @finally block and I have checked weather viewDidUnload is called in runtime and yes – it is called.

Does this mean I managed to unload my UIViewController object I created when navigating to it with modal segue from another UIViewController and remove it from memory?

Is this method regular as a replacement for:

[self dismissViewControllerAnimated:YES completion:nil];

because this above line returns me to UIViewController from which I navigated to current UIViewController, but that doesn't fit my needs, because I need to perform new segues from current UIViewController to other UIViewControllers (beside returning back to UIViewController from which I navigated to current one)?


[edit #2: Finish]

At the end I changed implementation model and loaded new UIViews under single UIViewController after I created separate XIB files for those UIViews. I have marked answer from @dasblinkenlight as the right one since it contains lots of useful informations and discussion on that answer gives good answers to some doubts about using modal segues.

Best Answer

I do not know the answer to the first part of your question, but once you learn the answer to the second part, I am sure that you would go with a different solution anyway.

viewDidLoad method is called every time. So, does that mean I created new object instance each time viewDidLoad method was executed?

Absolutely. "Modal" segue causes the new view to obscure the old one completely until the new view is closed. If you go back and forth many times, your code will accumulate a whole "stack" of views underneath the current one.

I also notice that viewDidUnload method is never called. So, if answer to previous question is affirmative (each viewDidLoad execution creates new object instance), does that mean that my UIViewController object instances are never being unloaded and deleted?

This is correct, all the view controllers that you create are still there, ready for you to close the views on top of it.

Or ARC is doing garbage collection behind the scenes?

ARC is not a garbage collector, it is a reference counting mechanism with a little automation from the compiler. The objects are still there.

You should change your code to call

[self dismissModalViewControllerAnimated:YES];

in the second controller, rather than using a modal segue that brings you back to the first one.

Related Topic