我有一个带有导航视图列表的应用程序,当稍后在应用程序中添加新元素时,该列表不会更新。初始屏幕很好,无论我如何编写它们,此时都会触发所有内容,但除此之外,它保持不变。在某些时候,我将我的“init”方法作为 .onappear,动态元素不会进入,但是当我在应用程序中来回移动时,静态元素会被添加多次,这不再是我现在的代码。
这是我的内容视图的样子,我试图将导航视图部分移动到具有已发布 var 的类,以防万一它有帮助,从视觉上它可以改变任何东西,也可以提供帮助。
struct ContentView: View {
@ObservedObject var diceViewList = DiceViewList()
var body: some View {
VStack{
Text("Diceimator").padding()
diceViewList.body
Text("Luck Selector")
}
}
}
和 DiceViewList 类
import Foundation
import SwiftUI
class DiceViewList: ObservableObject {
@Published var list = [DiceView]()
init() {
list.append(DiceView(objectID: "Generic", name: "Generic dice set"))
list.append(DiceView(objectID: "Add", name: "Add a new dice set"))
// This insert is a simulation of what add() does with the same exact values. it does get added properly
let pos = 1
let id = 1
self.list.insert(DiceView(objectID: String(id), dice: Dice(name: String("Dice"), face: 1, amount: 1), name: "Dice"), at: pos)
}
var body: some View {
NavigationView {
List {
ForEach(self.list) { dView in
NavigationLink(destination: DiceView(objectID: dView.id, dice: dView.dice, name: dView.name)) {
HStack { Text(dView.name) }
}
}
}
}
}
func add(dice: Dice) {
let pos = list.count - 1
let id = list.count - 1
self.list.insert(DiceView(objectID: String(id), dice: dice, name: dice.name), at: pos)
}
}
我正在开发最新的 Xcode 11,以防万一
编辑:根据建议编辑代码,问题根本没有改变
struct ContentView: View {
@ObservedObject var vm: DiceViewList = DiceViewList()
var body: some View {
NavigationView {
List(vm.customlist) { dice in
NavigationLink(destination: DiceView(dice: dice)) {
Text(dice.name)
}
}
}
}
}
和DiceViewList
班级
class DiceViewList: ObservableObject {
@Published var customlist: [Dice] = []
func add(dice: Dice) {
self.customlist.append(dice)
}
init() {
customlist.append(Dice(objectID: "0", name: "Generic", face: 1, amount: 1))
customlist.append(Dice(objectID: "999", name: "AddDice", face: 1, amount: 1))
}
}