使用NavigationLink Push UIViewControllerRepresentable时SwiftUI内存泄漏?

时间:2020-06-30 16:11:18

标签: swiftui

这种情况没问题。enter link description here

所以我很困惑,我不知道这是错误还是Apple是否以此方式设计。

struct HomePage: View {

    var body: some View {
        NavigationView {
            VStack {
                jumpView1
                    .padding()
                    .background(Color.red)
                    .padding()
                jumpView2
                    .padding()
                    .background(Color.blue)
            }
            .navigationBarItems(leading:leftBarItem)
            .navigationBarTitle("Home",displayMode: .inline)
        }
    }
    var leftBarItem: some View {
        NavigationLink(destination: CustomViewController()) {
               Text("Go CustomViewController ")
           }
    }
    var jumpView1: some View {
        NavigationLink(destination: CustomViewController()) {
                       Text("Go CustomViewController")
                        .foregroundColor(.white)
        }
    }
    var jumpView2: some View {
           NavigationLink(destination: SecondPage()) {
                          Text("Go SecondPage")
                            .foregroundColor(.white)
           }
    }
}
struct FirstPage: View {
    var body: some View {
        NavigationLink(destination: CustomViewController()) {
                       Text("Go CustomViewController")
        }
    }
}
struct SecondPage: View {
    var body: some View {
        NavigationLink(destination: CustomViewController()) {
                       Text("Go CustomViewController")
        }
    }
}

仅在leftBarItem Action中调用func dismantleUIViewController, 推入身体NavigatinLink时永远不会打电话

struct CustomViewController: UIViewControllerRepresentable {
    
    func makeUIViewController(context: Context) ->  CustomRealViewController {
        let vc = CustomRealViewController()
        return vc
    }
    func updateUIViewController(_ uiViewController: CustomRealViewController, context: Context) {
    }

    static func dismantleUIViewController(_ uiViewController: CustomRealViewController, coordinator: Coordinator) {
        print("dismiss")
        
    }
    
}

仅在leftBarItem Action中调用func deinit, 推入身体NavigatinLink时永远不会打电话

class CustomRealViewController: UIViewController {

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        print("viewDidDisappear")
    }
    deinit {
        print("deinit")
    }
}

0 个答案:

没有答案