首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIViewControllerRepresentable中与Storyboard的绑定

UIViewControllerRepresentable中与Storyboard的绑定
EN

Stack Overflow用户
提问于 2021-08-02 15:10:53
回答 1查看 287关注 0票数 0

我有一个UIViewControllerRepresentable和一个UIStoryboard。如何将@对象传递给ViewController?它目前还没有初始化,我无法将它传递给"as!ARView(模型:模型)“。

代码语言:javascript
复制
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)
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-02 16:42:55

没有理由在@ObservableObject中使用UIViewController。属性包装器不会像在View中那样对您有任何好处,从而触发更新。

@ObservableObject不能是Optional,但是由于它不再需要使用属性包装器,所以可以将它变成Optional。显然,当需要使用它时,您将不得不展开它。

代码语言:javascript
复制
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?
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68623991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档