andreamazz / BubbleTransition

A custom modal transition that presents and dismiss a controller with an expanding bubble effect.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

self.transitioningDelegate not retained

acegreen opened this issue · comments

Hi,

I'm running into a problem where transitioningDelegate is set the VC is presented with the custom transition but at some point, the transitioningDelegate is nil and so when its dismissed, the transition is the regular dismiss one.

I understand that transitioningDelegate is a weak property but the VC is not deinit'd as I retain a reference to it in my presenting VC

Note 1: when I check transitioningDelegate in the viewDidLoad() of the presented VC, its the assigned BubbleTransitionDelegate class as expected, but when I'm about to dismiss it, transitioningDelegate is nil

Note 2: the only thing I can think of that I'm doing differently, is that I'm extending UIViewController to have a new present(bubble VC)() method as such:

extension UIViewController {
    func present( bubble viewController: UIViewController, from target: UIView?, presentingColor: UIColor = OMColor.red, dismissingColor: UIColor = OMColor.white, completion: (() -> ())?) {
        
        let bubbleTransitionDelegate = BubbleTransitionDelegate(target: target, presentingColor: presentingColor, dismissingColor: dismissingColor)

        viewController.transitioningDelegate = bubbleTransitionDelegate
        viewController.modalPresentationStyle = .custom
        
        self.present(viewController, animated: true) { 
            completion?()
        }
    }
}

You need to store bubbleTransitionDelegate as a property. That instance will be deallocated as soon as the present method is completed. That's standard memory management, every object not retained declared in the scope of a function will be discarded when the function returns.

@andreamazz appreciate the response. Why is it that in this instance I need to specifically store the property? I have implemented such transitionDelegates (event this library) before without storing their property.

This is not a specific instance, it's how the runtime system works. UIKit declares the transitioningDelegate as a weak property, therefore if you don't hold the reference, no one will.
Maybe in other cases your delegate was retained by someone else.

@andreamazz I understand that but I have done the same animation before without having to retain my transitioningDelegate anywhere and it worked on dismiss

The delegate object was either retained by someone else or it was instantiated in both the presenting and presented VC, there's no way around how the runtime system works...