简体   繁体   中英

UIViewController isn't deinitialized

class MyViewController: UIViewController {
    // ...

    deinit {
        print("test deinit")
    }

    // ...

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

    // ...
}

This is (the part of) my view controller that always pushed in navigation controller when it present.

And I want it deinitialized when it popped out of navigation controller.

But It was never printed "test deinit" when it popped out by anything methods(call exit() , swipe ... etc).

And I guess the presenting method could make this issue. So I was test to presenting with below two methods. but I couldn't resolve it in any way.

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

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

or

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

What's the problem? How do I deinitialize it?

Check your closures, and delegates must be something wrong and your viewController is being retained

make sure that your delegates are declared as weak ,

weak var delegate: YourDelegateProtocol

and your closure that use self are execute either with [weak self] or [unowned self]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM