Ok Apple how to dismiss a SwiftUI Modal

I use UIHostingController inside my UIViewController to present a SwiftUI View. I have a button in that view which I want to tap and dismiss the presented view. How do I dimiss SwiftUI views when presented in a UIHostingController


...{
let vc = UIHostingController(rootView: SwiftUIView())
  present(vc, animated: true, completion: nil)
}



struct SwiftUIView : View {
  var body: some View {
  CustomButton()
  }  
 }

struct CustomButton: View {

  var body: some View {

  Button(action: {
  self.buttonAction()
  }) {
  Text(buttonTitle)
  }

  }

  func buttonAction(){
  //dismiss the SwiftUIView when this button pressed
  }

 }
Way 1:
Code Block
struct TestView: View {
  var dismiss: (() -> Void)
  var body: some View {
    Button(action: dismiss) {
      Text("Close")
    }
  }
}


Code Block
   func show() {
     let view = TestView(dismiss: { self.navigationController.presentedViewController?.dismiss(animated: true) })
    let vc = UIHostingController(rootView: view)
    navigationController.present(vc, animated: true)
  }

Way 2:
Code Block
struct TestView: View {
  var dismiss: (() -> Void)!
  var body: some View {
    Button(action: dismiss) {
      Text("Close")
    }
  }
}
final class TestViewController: UIHostingController<TestView> {
  init() {
    super.init(rootView: TestView())
    rootView.dismiss = dismiss
  }
  @objc required dynamic init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  func dismiss() {
    dismiss(animated: true, completion: nil)
  }
}

Code Block
   func show() {
    let vc = TestViewController()
    navigationController.present(vc, animated: true)
  }




Ok Apple how to dismiss a SwiftUI Modal
 
 
Q