正确的方法来删除UIViewController

时间:2017-03-03 19:54:18

标签: ios swift xcode uiviewcontroller uistoryboard

我有settingsController,这是一个自定义UINavigationController,必须出现和消失。

它的XIB在故事板中,由于其他管家的目的,我必须使用var settingsController来跟踪它。

主视图控制器:

class MainViewController: UIViewController {

    var settingsController : MySettingsNavigationController?

    func settingsButtonPressed () {
        let storyboard = UIStoryboard(name: "Main Storyboard", bundle: nil)
        self.settingsController = storyboard.instantiateViewController(withIdentifier: "Settings Navigation Controller") as? MySettingsNavigationController
        self.settingsController!.transitioningDelegate = self
        self.settingsController!.modalPresentationStyle = .custom
        self.present(self.settingsController!, animated: true, completion: nil)
    }

    //Settings View Controller Transition Methods
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        let containerView = transitionContext.containerView
        let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)
        let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
        if(self.isSettingsPresenting == true) {
            toViewController!.view.alpha = 1;
            toViewController!.view.transform = CGAffineTransform(scaleX: 0.001, y: 0.001);
            toViewController!.view.center = self.settingsButton.center

            UIView.animate(withDuration: 0.5, delay: 0,
                           options: [.curveEaseIn], animations: { () -> Void in

                toViewController!.view.alpha = 1;
                toViewController!.view.transform = CGAffineTransform(scaleX: 1, y: 1);
                toViewController!.view.center = fromViewController!.view.center
                containerView.addSubview(toViewController!.view)

            }, completion: { (completed) -> Void in

                transitionContext.completeTransition(completed)

            })

        } else {
            UIView.animate(withDuration: 0.5, delay: 0,
                           options: [.curveEaseOut], animations: { () -> Void in

                fromViewController!.view.alpha = 0;
                fromViewController!.view.transform = CGAffineTransform(scaleX: 0.01, y: 0.01);
                fromViewController!.view.center = self.settingsButton.center

            }, completion: { (completed) -> Void in
                //Two lines below added later to test..
                fromViewController?.view.removeFromSuperview()
                fromViewController?.removeFromParentViewController()
                transitionContext.completeTransition(completed)
            })
        }
    }
}

设置导航控制器:

class MySettingsNavigationController:  UINavigationController, UINavigationControllerDelegate
{
    .....

    func closeButtonPressed (_ sender: UIButton!) {
        self.dismiss(animated: true, completion: {
            //Tried removing self in here...
        })
    }
}

问题:

当我调出settingsController并关闭它时,settingsController分配的内存永远不会释放;不知何故,settingsController没有被解除分配。

注意:

不知怎的,我必须:

  1. 从故事板启动视图控制器
  2. 使用自定义动画过渡
  3. 不使用自定义委托(因为我不能写委托给什么 已经是UINavigationControllerDelegate了。这本来可以 我有机会回电给父母,MainViewController就是这样 准备好被删除

1 个答案:

答案 0 :(得分:3)

  

不知何故,settingsController没有被取消分配

  • var settingsController看起来像是可能的罪魁祸首 持有ViewController作为var对象非常气馁
  

我必须使用' var settingsController'来跟踪它。由于其他管家的目的。

  • 使用提供sharedHelper相关属性数据的controller - esque类进行评估,或处理用户互动。
  • 除了settingsButtonPressed(单独应该负责调用settingsController)之外,没有任何函数可以设计为需要settingsController的实例才能执行。此类函数所需的数据可通过多种方式提取,如protocolsblocksshared instances等。