UIViewControllerRepresentable奇怪的释放

时间:2020-11-11 17:10:36

标签: swiftui

我有一个UIViewController,它在SwiftUI中由UIViewControllerRepresentable使用。

当我从控制器导航回到SwiftUI视图时,不会调用deinit(正如我期望的那样)。

但是,当我对UIViewControllerRepresentable执行新的导航时,首先调用deinit(对于之前的控制器)

UIViewControllerRepresentable在这里缺少什么,为什么不取消分配呢?

import SwiftUI
import UIKit

class MyViewController: UIViewController {

    deinit() {
        print("This isn't called until the second initiation of the UIViewControllerRepresentable")
    }

}

struct MyViewControllerWrapper: UIViewControllerRepresentable {
    typealias UIViewControllerType = MyViewController

    func makeUIViewController(context: UIViewControllerRepresentableContext<MyViewControllerWrapper>) -> MyViewControllerWrapper.UIViewControllerType {
    
        let vc = UIViewControllerType(requestType: requestType)
        return vc
    }

    func updateUIViewController(_ uiViewController: MyViewControllerWrapper.UIViewControllerType, context: UIViewControllerRepresentableContext<MyViewControllerWrapper>) {
    
    }
}

0 个答案:

没有答案