简体   繁体   中英

How to access a SwiftUI view from its parent UIViewController?

I am using a SwiftUI view from a UIViewController using UIHostingController.

This is not working:

viewController.contentView.first as? SwiftUIView

Because it's not a SwiftUI view anymore, but a UIView.

Is it possible to get back the SwiftUI view from the UIViewController?

Just save the reference on a view controller's variable to the SwiftUI view when you create it.

class ViewController: UIViewController {
  // Store the reference to the SwiftUI view after creating it.
  var swiftUiView: SwiftUiView?
  // ...
}

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