首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIViewControllerRepresentable要求类型“”some View“”和“”Never“”等效

UIViewControllerRepresentable要求类型“”some View“”和“”Never“”等效
EN

Stack Overflow用户
提问于 2019-09-12 19:26:11
回答 1查看 1.4K关注 0票数 5

使用Xcode11GM种子编写一些SwiftUI代码,我遇到了一个我不理解的Swift错误。

代码语言:javascript
复制
struct MainViewController: View {
    var body: some View {
        VStack {
            Text("Hello World!")
        }
    }
}

extension MainViewController : UIViewControllerRepresentable {
    func makeUIViewController(context: UIViewControllerRepresentableContext<MainViewController>) -> UINavigationController {
        return UINavigationController()
    }

    func updateUIViewController(_ uiViewController: UINavigationController, context: UIViewControllerRepresentableContext<MainViewController>) {

    }
}

这将报告:

代码语言:javascript
复制
'UIViewControllerRepresentable' requires the types 'some View' and 'Never' be equivalent
EN

回答 1

Stack Overflow用户

发布于 2019-09-12 19:58:13

我错过了ViewController和视图之间的分离。错误是说视图控制器不能有返回视图的主体。

这是可行的:

代码语言:javascript
复制
struct MainView : View {
    var body: some View {
        VStack {
            Text("Hello World!")
        }
    }
}

struct MainViewController : UIViewControllerRepresentable {
    func makeUIViewController(context: UIViewControllerRepresentableContext<MainViewController>) -> UIHostingController<MainView> {
        return UIHostingController(rootView: MainView())
    }

    func updateUIViewController(_ uiViewController: UIHostingController<MainView>, context: UIViewControllerRepresentableContext<MainViewController>) {

    }
}

然后实例化它:

代码语言:javascript
复制
let viewController = UIHostingController<MainViewController>(rootView:MainViewController())
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57905693

复制
相关文章

相似问题

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