【问题标题】:UIViewController isn't deinitializedUIViewController 没有被取消初始化
【发布时间】:2018-02-20 11:02:27
【问题描述】:
class MyViewController: UIViewController {
    // ...

    deinit {
        print("test deinit")
    }

    // ...

    func exit() {
        self.navigationController?.popViewController(animated: true)
    }

    // ...
}

这是我的视图控制器的(部分),当它出现时总是推入导航控制器。

我希望它在从导航控制器中弹出时取消初始化。

但是当它被任何方法弹出时(调用exit(),刷卡......等),它从未打印过“test deinit”。

我猜呈现方法可能会导致这个问题。所以我测试了以下两种方法。但我无法以任何方式解决它。

let controller = UIStoryboard(name: "MyStoryboard").instantiateViewController(withIdentifier: "MyViewController") as! MyViewController

self.navigationController?.pushViewController(controller, animated: true)

self.performSegue(withIdentifier: "ToMyVC", sender: nil)

有什么问题?如何取消初始化?

【问题讨论】:

  • 检查你的闭包,委托一定有问题,你的viewController被保留了
  • @ReinierMelian 谢谢。这是正确的。我没有被放在[unowned self] 的众多闭包中。
  • 你也可以使用[弱自我]
  • 添加为@Byoth的答案

标签: ios swift uiviewcontroller uinavigationcontroller


【解决方案1】:

检查你的闭包,代表一定有问题,你的viewController被保留

确保您的代表被声明为weak

weak var delegate: YourDelegateProtocol

您使用self 的闭包将使用[weak self][unowned self] 执行

let yourClosure : (()->Void) = { [weak self] in
    //your closure implementation
}

【讨论】:

    猜你喜欢
    • 2019-09-18
    • 1970-01-01
    • 2020-02-11
    • 2019-07-14
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    相关资源
    最近更新 更多