【问题标题】:SwiftUI - List doesn't update when I transfer data from view to anotherSwiftUI - 当我将数据从视图传输到另一个视图时,列表不会更新
【发布时间】:2020-06-21 20:24:58
【问题描述】:

我正在尝试将数据从 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 !!")
        }
    }

【问题讨论】:

    标签: swift xcode swiftui swiftui-navigationlink


    【解决方案1】:

    MyMoodList 结构的NavigationView 上使用.onAppear 修饰符将字符串添加到数组中。

    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)
            }
        }
    }}
    

    【讨论】:

    • 我要修改整个答案。
    • 检查新答案
    • 非常感谢,我会检查并通知您。
    • 每次我打开 MyMoodList 它都会添加新行。所以我更新了我的问题。你认为它现在有效吗?。
    • 在这种情况下,您可以检查数组是否包含元素。检查更新的答案我使用contains 作为数组。无需新的state var 进行检查。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    • 2020-06-30
    • 2018-03-06
    • 1970-01-01
    相关资源
    最近更新 更多