UIViewControllerRepresentable 与 Storyboard 的绑定
Bindings in UIViewControllerRepresentable with Storyboard
我有一个可以用 UIStoryboard 表示的 UIViewController。如何将我的@ObservedObject 传递给 ViewController?目前尚未初始化,我无法将其传递给“as!ARView(model:model)”
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)
}
没有理由在 UIViewController
中使用 @ObservableObject
。 属性 包装器不像在 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?
}
我有一个可以用 UIStoryboard 表示的 UIViewController。如何将我的@ObservedObject 传递给 ViewController?目前尚未初始化,我无法将其传递给“as!ARView(model:model)”
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)
}
没有理由在 UIViewController
中使用 @ObservableObject
。 属性 包装器不像在 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?
}