0

I have a loading view controller when my app starts, when an animation in this view controller finished I want it to show another view controller and dismiss the view controller with the animation.

The loading view controller is the initial view controller,

I have this code when UIStoryboard.mflMainTabBarViewController(). returns the view controller that I want to present

func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
    let animationID = anim.value(forKey: "animationID")
    if animationID as! NSString == "transform" {
        self.present(UIStoryboard.mflMainTabBarViewController(), animated: true, completion: {
            _ = self.popoverPresentationController
        })

    }
}`

But when deinit is never called

    deinit {
    print("deinit")
}

What is the best way to deinit the first view controller, and making the presenting view controller the root view controller?

dmram
  • 125
  • 1
  • 12

3 Answers3

0

When you are using weak reference cycle at that time deinit method is calling. In strong reference cycle deinit not calling. So create weak reference cycle for calling the method.

Also refer this link.

Shah Nilay
  • 778
  • 3
  • 21
  • even when I add weak var weakSelf = self _ = weakSelf?.popoverPresentationController deinit isnt called – dmram Aug 09 '17 at 05:23
  • its the same, I just replaced self. with weakSelf when doing the popover – dmram Aug 09 '17 at 05:42
0

try this.

Use the vc's parent to present.

And dismiss the vc itself.

self.presentingViewController?.present(UIStoryboard.mflMainTabBarViewController(), animated: true, completion: {
            _ = self.popoverPresentationController
        })
self.dismiss(animated: false, completion: nil)

or

self.navigationController?.present(UIStoryboard.mflMainTabBarViewController(), animated: true, completion: {
            _ = self.popoverPresentationController
        })
self.navigationController?.popViewController(animated: false)
halftrue
  • 149
  • 2
  • 7
0

If you are 100% sure that self will never be nil, then just use

    func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
        let animationID = anim.value(forKey: "animationID")
        if animationID as! NSString == "transform" {
            self.present(UIStoryboard.mflMainTabBarViewController(), animated: true, completion: { [unowned self] in
                _ = self.popoverPresentationController
            })

        }
 }`

weak and unowned are the same. Except one thing. Unlike weak we’ve seen, unowned does not automatically convert self as optional within the closure block.

Tushar Sharma
  • 2,839
  • 1
  • 16
  • 38