【发布时间】: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 控制器之前添加一个导航控制器,但我只是得到一个黑屏。
【问题讨论】: