gpt4 book ai didi

ios - 删除 UIViewController 的正确方法

转载 作者:搜寻专家 更新时间:2023-11-01 06:34:51 26 4
gpt4 key购买 nike

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

它的 XIB 在 Storyboard中,由于其他内务管理目的,我必须使用 var settingsController 跟踪它。

主视图 Controller :

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)
})
}
}
}

设置导航 Controller :

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

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

问题:

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

注意:

不知何故我必须:

  1. 从 Storyboard启动 View Controller
  2. 使用自定义动画过渡
  3. 不使用自定义委托(delegate)(因为我不能将委托(delegate)写到什么已经是一个 UINavigationControllerDelegate。这本可以给我有机会回电父 MainViewController 它是准备好被移除

最佳答案

Somehow, settingsController is not getting deallocated

  • var settingsController看起来很可能是罪魁祸首。
    坚持ViewController作为var对象非常气馁

I have to keep track of it using 'var settingsController' due to other house keeping purposes.

  • 使用 sharedHelper 进行评估- 提供 controller 的类相关属性数据或处理用户交互。
  • 没有功能,除了settingsButtonPressed (单独应负责调用 settingsController ),应设计为需要 settingsController 的实例执行。这些功能所需的数据可以通过多种方式提取,例如 protocols , blocks , shared instances

关于ios - 删除 UIViewController 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42587344/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com