【问题标题】:Calling SwiftUI View from UITabBarController从 UITabBarController 调用 SwiftUI 视图
【发布时间】:2020-05-12 05:09:21
【问题描述】:

我正在慢慢地将我的项目转换为 SwiftUI。我想将情节提要中的 UITabBarController 连接到 SwiftUI 视图。

我的理解是最好的方法是使用 UIHostingController。我在情节提要中添加了一个,将其连接到 TabBar,并将我的自定义 HostingViewController 作为自定义类添加到该控制器。 (如果这很重要,这确实会显示在“更多”选项卡下)

我假设我在这里遗漏了一些代码,但我发现主要是 sn-ps 缺少适当的示例。 UIHostingController

import Foundation
import UIKit
import SwiftUI

class HseEventHostingVC: UIHostingController<HseView>{

}

我已将其设置为故事板中 UIHostingController 的类,该类连接到我的标签栏。当我尝试运行时,我收到此错误。

致命错误:init(coder:) 必须在子类中实现并调用 super.init(coder:, rootView:): 文件

我遵循了这个简单的指南here,这是为了推送一个 SwiftUI 视图,但我认为它在理论上并没有太大的不同。

这是我的 SwiftUI 视图

import SwiftUI

struct HseView: View {
    var body: some View {
        let first = HSECell(name: "Newest Observation", duedate: "2019/10/10", status: "Open", reportedBy: "Carson Skjerdal", reportedDate: "2020/01/01", hseid: "OBS12")

        let hseItems = [first]

        return List(hseItems) {item in

            HseItemRow(hsecell: item)
        }
    }
}

struct HseView_Previews: PreviewProvider {
    static var previews: some View {
        HseView()
    }
}

struct HseItemRow: View{

    var hsecell: HSECell

    var body: some View{
        VStack{
            HStack(){
                Text("\(hsecell.hseid)")
                Text("\(hsecell.name)")
                    .bold()
            }
            Divider()
            HStack{
                VStack(alignment: .leading){
                    Text("Reported Date: ")
                        .bold()
                    Text("Reported By: ")
                        .bold()
                    Text("Status: ")
                        .bold()
                    Text("Due Date:")
                        .bold()
                }
                VStack(alignment: .leading){

                    Text("\(hsecell.reportedDate)")

                    Text("\(hsecell.reportedBy)")

                    Text("\(hsecell.status)")

                    Text("\(hsecell.duedate)")
                }
            }.padding(.trailing)

        }
    }


}

还有我的 HSECell 可识别结构

import Foundation

struct HSECell: Identifiable{

    var id = UUID()
    var name: String
    var duedate: String
    var status: String
    var reportedBy: String
    var reportedDate: String
    var hseid: String 

}

更新:我尝试在 Hosting 控制器之前添加一个导航控制器,但我只是得到一个黑屏。

【问题讨论】:

    标签: ios swiftui


    【解决方案1】:

    能够解决这个问题,我认为其他人最终会需要它。

    关键是在定义我的 UIHostController 时,我必须使用我的 SwiftUI 视图正确初始化它

    class MyClassNameHostVC: UIHostingController<CustomSwiftUiView>{
        required init?(coder aDecoder: NSCoder){
            super.init(coder: aDecoder, rootView: CustomSwiftUiView())
        }
    
    } 
    

    this site..帮助解决了这个问题。

    【讨论】:

    • 很高兴您解决了问题。我正面临同样的黑屏,但我的错误是我没有在情节提要中替换 UIHostingController 的自定义类。
    • 这成功了,很高兴你找到了答案,这对我帮助很大!这几天一直在努力
    猜你喜欢
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    • 2020-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    相关资源
    最近更新 更多