简体   繁体   中英

Swift iOS -storyboard.instantiateViewController(withIdentifier:) causing retain cycle

I'm using my physical device and not the simulator.

I'm instantiating a vc using storyboard.instantiateViewController(withIdentifier:) and presenting it modally. I dismiss it using presentingViewController?.dismiss(animated: true, completion: nil) . Inside the presented vc I have a print method inside Deinit that never runs.

I went to Instruments > Allocations > Statistics > Allocation Summary > MyApp.ThePresenedController and it shows 2 faces saying something is wrong. When I clicked them it took me to the presenting vc's code where I instantiated the vc to present and highlighted it green. After the presented vc is dismissed it's not removed from the Allocation Summary list. Inside the presented vc there isn't a reference to the presenting vc so it's not a weak var problem.

在此处输入图片说明

在此处输入图片说明

How come storyboard.instantiateViewController(withIdentifier:) is causing me a problem?

Presenting VC:

@IBAction func forgotPasswordButtonTapped(_ sender: UIButton) {

    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let forgotPasswordVC = mainStoryboard.instantiateViewController(withIdentifier: "ForgotPasswordController") as! ForgotPasswordController
    let navVC = UINavigationController(rootViewController: forgotPasswordVC)
    present(navVC, animated: true, completion: nil)
}

Presented VC:

@IBAction func cancelButtonTapped(_ sender: UIButton) {

    presentingViewController?.dismiss(animated: true, completion: nil)
}

deinit{
    print("I've been dismissed")
}

I'm also using the same storyboard.instantiateViewController(withIdentifier:) code inside AppDelegate and the same 2 faces and highlighted green error is occurring.

AppDelegate didFinishLaunching:

let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

if userDoesThis {

    // if true this first line will highlight green
    let thisVC: ThisController = mainStoryboard.instantiateViewController(withIdentifier: "ThisController") as! ThisController
    let nav = UINavigationController(rootViewController: thisVC)

} else {

    // if false this first line will highlight green
    let thatVC: ThatController = mainStoryboard.instantiateViewController(withIdentifier: "ThisController") as! ThatController
    let nav = UINavigationController(rootViewController: thatVC)
}

window?.rootViewController = nav
window?.makeKeyAndVisible()
return true

As @StevenFisher suggested inside the comments the problem wasn't the green highlighted line itself but instead was a closure that I overlooked and didn't declare with [weak self] . I've read articles that says that pressing the faceless faces takes you to the offending line of code but Steve pointed it that might not be the issue and it can/will instead take you to where the problem starts. In my situation it let me know that I had a problem somewhere in the file as soon as it was instantiated and not the line itself.

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