【发布时间】:2020-02-11 01:40:00
【问题描述】:
我正在使用领域结果集填充列表。
从该列表导航时,它会打开一个新视图,然后自动关闭该视图。
使用结构没有问题。
为什么第二个视图会自动关闭?
我有一个屏幕录像,但不能在这里发帖。
import SwiftUI
import Combine
struct TestStruct:Identifiable{
let id = UUID()
let firstname: String
}
extension TestStruct {
static func all() -> [TestStruct]{
return[
TestStruct(firstname: "Joe"),
TestStruct(firstname: "Jane"),
TestStruct(firstname: "Johns")
]
}
}
struct TestListView: View {
let realmList = Horoscope.getHoroscopes() //Fetches from Realm
let structList = TestStruct.all()
var body: some View {
NavigationView{
// This owrks
// List(structList) { item in
// MyItemRow(itemTxt: item.firstname)
// }
//This automatically closes the view
List(realmList) { item in
MyItemRow(itemTxt: item.firstname)
}
.navigationBarTitle("Charts", displayMode: .automatic)
.navigationBarItems(trailing: EditButton())
}
}
}
struct MyItemRow: View {
var itemTxt:String
var body: some View {
NavigationLink(destination: Text("Test")) {
Text(itemTxt)
}
}
}
struct TestListView_Previews: PreviewProvider {
static var previews: some View {
TestListView()
}
}
【问题讨论】:
-
这里几乎没有错误检查,所以 item.firstname 可能为 nil,realmList 可能为空。另外,我认为 NavigationLink 需要嵌入到 NavigationView 中。
-
TestStruct.firstname不能为零,它不是可选的。空列表适用于List和ForEach。NavigationLink在MyItemRow内,它嵌入在NavigationView中的TestListView中。 -
好点。我更多地指的是
let realmList = Horoscope.getHoroscopes(),但你指出的其他一切都是正确的。
标签: realm swiftui navigationlink