Home > front end >  Correct way to use UIViewControllerRepresentable
Correct way to use UIViewControllerRepresentable

Time:08-22

I usually use it this way because if I create a new object in makeUIViewController there are two objects in total. So instead of creating new object I return self. Do you think I am doing it right?

final class MyViewController: UIViewController, UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> MyViewController {
        return self
    }

    func updateUIViewController(_ uiViewController: MyViewController, context: Context) {}
}

CodePudding user response:

Representable must be a struct, the pattern is

struct MyView: UIViewControllerRepresentable { // << view

    func makeUIViewController(context: Context) -> MyViewController {
        return MyViewController()  // create controller !!
    }

    func updateUIViewController(_ uiViewController: MyViewController, context: Context) {}
}
  • Related