我有一个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没有被释放。

注意:

我必须以某种方式:
  • 从情节提要
  • 启动视图控制器
  • 使用自定义动画过渡
  • 不要使用自定义委托(因为我无法将委托写入
    已经是UINavigationControllerDelegate了。这本可以给
    我有机会回给父母,MainViewController
    准备好删除了
  • 最佳答案

    不知何故,settingsController没有被释放

  • var settingsController看起来像是罪魁祸首。
    强烈建议不要将ViewController作为var对象。

  • 由于其他内部管理目的,我必须使用'var settingsController'来跟踪它。
  • 利用sharedHelper-esque类进行评估,该类提供controller相关的属性数据或处理用户交互。
  • settingsButtonPressed(仅应由其负责调用settingsController)外,没有其他函数应设计为需要执行settingsController的实例。此类功能所需的数据可以通过protocolsblocksshared instances等多种方式提取。
  • 09-19 02:19