Leaking views when changing rootViewController inside transitionWithView

前端 未结 5 1714
陌清茗
陌清茗 2020-12-04 05:29

While investigating a memory leak I discovered a problem related to the technique of calling setRootViewController: inside a transition animation block:

5条回答
  •  [愿得一人]
    2020-12-04 06:12

    I try a simple thing which work for me on iOs 9.3 : just remove the old viewController's view from its hierarchy during dismissViewControllerAnimated completion.

    Let's work on X, Y, and Z view as explained by benzado :

    That is, this sequence of operations...

    1. X becomes Root View Controller
    2. X presents Y, so that Y's view is on screen
    3. Using transitionWithView: to make Z the new Root View Controller

    Which give :

    ////
    //Start point :
    
    let X = UIViewController ()
    let Y = UIViewController ()
    let Z = UIViewController ()
    
    window.rootViewController = X
    X.presentViewController (Y, animated:true, completion: nil)
    
    ////
    //Transition :
    
    UIView.transitionWithView(window,
                              duration: 0.25,
                              options: UIViewAnimationOptions.TransitionFlipFromRight,
                              animations: { () -> Void in
                                    X.dismissViewControllerAnimated(false, completion: {
                                            X.view.removeFromSuperview()
                                        })
                                    window.rootViewController = Z
                               },
                               completion: nil)
    

    In my case, X and Y are well dealloc and their's view are no more in hierarchy !

提交回复
热议问题