1

我正在尝试将数据从 FirstView 发送到名为 myMoodList 的结构中的列表。我的问题是一旦数据发送到 myMoodList,它就不会添加到列表中。所以我在导航栏中放置了虚拟按钮,这样我就可以添加包含数据的行。

这是我的代码,

var body: some View {
    NavigationView{
        VStack{
            Text(currentMood)

}
    }.navigationBarItems(trailing: NavigationLink(destination:MyMoodList(currentMood: currentMood)) {
        Text("Next")
    }
    ).navigationBarTitle("Mood People", displayMode: .inline)
}

所以 currentMood 是 String 并且它的值是“Happy”。

这是我的主要观点。

struct moodStructure: Identifiable,Decodable {

let id = UUID()
let myMood: String
}

class MoodViewModel: ObservableObject {
@Published var moodStatus = "Test on MoodObjectModel"
@Published var moodArray: [moodStructure] = [
    .init(myMood: "Happy"),
    .init(myMood: "Sad")
]
func addItem(moodStatus: String) {
    moodArray.append(moodStructure(myMood: moodStatus))
    print(moodArray.capacity)
}
func didChange() {
    self.moodStatus = "Test inside did change"
}
}


 struct MyMoodList: View {


@ObservedObject var moodVM = MoodViewModel()
@State var currentMood: String

var body: some View {

    NavigationView {
        List() {
            ForEach(moodVM.moodArray,id: \.myMood) { item in

                HStack {
                    VStack {
                        Text(item.myMood)
                        Text("Feb/3/2020")
                            .font(.callout)
                    }
                    Spacer()
                    Text(">")
                        .fontWeight(.semibold)
                }
            }

        }.navigationBarTitle("Tracker")
            .listStyle(GroupedListStyle())
            .navigationBarItems(trailing: Button(action: {
                self.moodVM.addItem(moodStatus: self.currentMood)
            }) {
                Image(systemName: "plus")
            })
    }
}}

所以 MyMoodList 结构有 navigationBarButton,我放它只是为了确保数据已成功从第一个视图发送到 MyMoodList。但我想要的是一旦用户从 firstView 移动到 MyMoodList,它会自动添加到列表中。我不想使用该按钮,因此可以将其添加到列表中。

我不知道列表是否不会自动更新,或者列表在将项目添加到类 MoodViewModel 中的 moodArray 之前先加载。

谢谢你。

更新:

在我的 FirstView 我有

.navigationBarItems(trailing: NavigationLink(destination:MyMoodList(currentMood: currentMood, addNewPost: true))

在我添加的 MyMoodList 类上@State var addNewPost: Bool

.onAppear() {
        if self.addNewPost == true {
            self.moodVM.addItem(moodStatus: self.currentMood)
            self.addNewPost = false
        } else {
            print("Nothing !!")
        }
    }
4

1 回答 1

0

利用 。结构上的onAppear修饰符以将字符串添加到数组中。NavigationViewMyMoodList

struct ContentView: View {
var currentMood = "Happy1"
var body: some View {
    NavigationView {
        VStack{
            Text(currentMood)

            NavigationLink(destination:MyMoodList(currentMood: currentMood)) {
                Text("Next")
            }
        }
        .navigationBarTitle("Mood People", displayMode: .inline)
    }
}}

struct moodStructure: Identifiable,Decodable {
  let id = UUID()
  let myMood: String
}

class MoodViewModel: ObservableObject {
    @Published var moodStatus = "Test on MoodObjectModel"
    @Published var moodArray: [moodStructure] = [
        .init(myMood: "Happy"),
        .init(myMood: "Sad")
    ]
    func addItem(moodStatus: String) {
        moodArray.append(moodStructure(myMood: moodStatus))
        print(moodArray.capacity)
    }
    func didChange() {
        self.moodStatus = "Test inside did change"
    }
  }

struct MyMoodList: View {

@ObservedObject var moodVM = MoodViewModel()
var currentMood: String

var body: some View {

    NavigationView {
        List() {
            ForEach(moodVM.moodArray,id: \.myMood) { item in

                HStack {
                    VStack {
                        Text(item.myMood)
                        Text("Feb/3/2020")
                            .font(.callout)
                    }
                    Spacer()
                    Text(">")
                        .fontWeight(.semibold)
                }
            }

        }.navigationBarTitle("Tracker")
            .listStyle(GroupedListStyle())
            .navigationBarItems(trailing: Button(action: {
                self.moodVM.addItem(moodStatus: self.currentMood)
            }) {
                Image(systemName: "plus")
            })
    }
    .onAppear {
        if !self.moodVM.contains(self.currentMood) {
        self.moodVM.addItem(moodStatus: self.currentMood)
        }
    }
}}
于 2020-03-10T05:46:14.973 回答