Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Photo Browser iPhone App View Controller Transitions Implementing Full Screen Transitions

kennedy otis
kennedy otis
3,570 Points

IOS Custom Transitions

Hi Am just concerned about

    - (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:  (UIViewController *)presenting sourceController:(UIViewController *)source
  {
    return [[CustomViewControllerTransition alloc] init];
   }

Does this mean every time you transit to detail view, a new instance is being created?

4 Answers

Thomas Nilsen
Thomas Nilsen
14,957 Points

I think so, yes. What you can do though, is something like this instead:

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    static PresentDetailTransition *transition = nil;
    if (transition == nil) {
        transition = [[PresentDetailTransition alloc] init];
    }
    return transition;
}

This principle is called lazy loading. Putting the keyword static in front of a local variable declaration creates a special type of variable. This variable keeps its value even after the method ends. The next time you call this method, the variable isn’t created, but the existing one is used.

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

I would advise against creating a static variable because you are keeping the transition in memory. The delegate for the animated transition is only called when you are actually transitioning, yes an instance is created but as soon as the transition is complete the instance is also destroyed. By creating a static variable you are keeping that instance in memory when you could use the memory for other functions.

kennedy otis
kennedy otis
3,570 Points

Thanks. This makes sense now and I think its an efficient approach.

kennedy otis
kennedy otis
3,570 Points

Ok.. So I can switch back to just returning an instance of the class. I was just curious about that part. Thanks Amit Bijlani