我有一个UIViewControllerRepresentable和一个UIStoryboard。如何将@对象传递给ViewController?它目前还没有初始化,我无法将它传递给"as!ARView(模型:模型)“。
struct ARViewContainer: UIViewControllerRepresentable {
@ObservedObject var model: Model
typealias UIViewControllerType = ARView
func makeUIViewController(context: Context) -> ARView {
UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(identifier: "Main") as! ARView
}
func updateUIViewController(_ uiViewController: ARViewContainer.UIViewControllerType, context: UIViewControllerRepresentableContext<ARViewContainer>) { }
}
class ARView: UIViewController, ARSCNViewDelegate {
// MARK: Object model
@ObservedObject var model: Model
// MARK: - Initalisation
init(model: Model) {
self.model = model
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}发布于 2021-08-02 16:42:55
没有理由在@ObservableObject中使用UIViewController。属性包装器不会像在View中那样对您有任何好处,从而触发更新。
@ObservableObject不能是Optional,但是由于它不再需要使用属性包装器,所以可以将它变成Optional。显然,当需要使用它时,您将不得不展开它。
struct ARViewContainer: UIViewControllerRepresentable {
@ObservedObject var model: Model
typealias UIViewControllerType = ARView
func makeUIViewController(context: Context) -> ARView {
let vc = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(identifier: "Main") as! ARView
vc.model = model
return vc
}
func updateUIViewController(_ uiViewController: ARViewContainer.UIViewControllerType, context: UIViewControllerRepresentableContext<ARViewContainer>) {
}
}
class ARView: UIViewController, ARSCNViewDelegate {
var model: Model?
}https://stackoverflow.com/questions/68623991
复制相似问题