我有一个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
的实例。此类功能所需的数据可以通过protocols
,blocks
,shared instances
等多种方式提取。