Custom iOS 7 UIViewController transition save loop

I create custom transitions in my application and run into two problems. If I installed a view controller to handle both UIViewControllerAnimatedTransitioning and UIViewControllerTransitioningDelegate , I ran into a problem with my view controller, which is never freed. In particular, this creates a persistence:

 self.transitioningDelegate = self; 

If I do not, put the UIViewControllerAnimatedTransitioning and UIViewControllerTransitioningDelegate in a separate NSObject called TransitionController and set it like this:

 self.transitioningDelegate = [[TransitionController alloc] init]; 

UIViewController freed , but I get memory leaks in the TransitionController object. Anyone now what am I doing wrong?

+8
ios objective-c uiviewcontroller
source share
5 answers

I ran into the same problem and was able to solve it.
The custom transition API is not well documented and requires a lot of trial and error so that I can fix it.

Let me tell you how I was able to get it working without memory problems:

Here are the players:

VCA = view controller that wants to introduce VCB modally
VCB = Modified View Controller (represented by VCA)

TC = Custom transition controller object that performs custom animation.
A subclass of NSObject that matches "UIViewControllerAnimatedTransitioning".
An instance will be created inside TD.

TD = Custom transition delegate object that the transition controller provides to the system. A subclass of NSObject that matches "UIViewControllerTransitioningDelegate"

Now imagine a VCB instance

self = VCA instance
myModalViewController = is a strong self property

 self.myModalViewController = [[VCB alloc] init]; [self.myModalViewController setModalPresentationStyle: UIModalPresentationCustom]; [self.myModalViewController setTransitioningDelegate: [[TD alloc] init]]; [self presentViewController: self.myModalViewController animated:YES completion:NULL]; 

At some point later, the VCB asks to reject the VCA

self = VCA instance
myModalViewController = VCB Instance Presented Above

 [self dismissViewControllerAnimated:YES completion:^{ [self.myModalViewController setTransitioningDelegate: nil]; // DO THIS!!!! self.myModalViewController = nil; }]; 



Hope this helps. This is definitely for me.

+6
source share

In my case, I myself ( UIViewController ) held an instance of a custom UIViewController (lets call it mViewController ), and self is a transitioningDelegate to show / reject mViewConroller . My solution to avoiding the save loop was to call it inside .m mViewController :

 -(void)viewDidDisappear:(BOOL)animated { self.transitioningDelegate = nil; [super viewDidDisappear:animated]; } 

Worked like a charm (:

+2
source share

In the second attempt, you allocate an instance of the TransitionController and it will never be released (since no one is referencing it). Objects should not store delegates in Objective-C, so you need to have a link to your ViewController and delegate to it from another point in your code.

0
source share

The UIViewControllerAnimatedTransitioning object UIViewControllerAnimatedTransitioning saved using the "from" VC after the transition is completed in iOS7 (this does not happen in iOS8), which may cause a memory leak if your transition object stores anything in the properties. This is a bit of me in the past, you need to be careful.

0
source share

In my case, what caused the persistence of my submitted view controller was that I passed the wrong logic code to the completion block of my animation.

 [transitionContext completeTransition:transitionContext.transitionWasCancelled]; 

It should be like this:

 BOOL successful = transitionContext.transitionWasCancelled == NO; [transitionContext completeTransition:successful]; 

Splitting the code into two lines helps readability.

0
source share

All Articles