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 trialkennedy otis
3,570 PointsIOS 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
14,957 PointsI 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
Treehouse Guest TeacherI 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.
Thomas Nilsen
14,957 PointsGood point!
kennedy otis
3,570 PointsThanks. This makes sense now and I think its an efficient approach.
kennedy otis
3,570 PointsOk.. So I can switch back to just returning an instance of the class. I was just curious about that part. Thanks Amit Bijlani