我正在尝试将数据从FirstView发送到名为myMoodList的结构中的List。 我的问题是,一旦数据发送到myMoodList,它就不会添加到列表中。 因此,我在NavigationBar中放置了虚拟按钮,以便可以添加包含数据的行。
这是我的代码,
If let storyboard = storybord{
let vc = storyboard.instantiateViewController(withIdentifier: ”viewcontroller-id”) as! your_VC
self.present(vc, animated: true)
}
所以currentMood是String,其值是“ Happy”。
这是我的主要观点。
var body: some View {
NavigationView{
VStack{
Text(currentMood)
}
}.navigationBarItems(trailing: NavigationLink(destination:MyMoodList(currentMood: currentMood)) {
Text("Next")
}
).navigationBarTitle("Mood People", displayMode: .inline)
}
因此MyMoodList结构具有navigationBarButton,我只是为了确保数据已成功从第一个视图发送到MyMoodList。 但是我想要的是,一旦用户从firstView移到MyMoodList,它将自动添加到列表中。我不想使用该按钮,因此可以将其添加到列表中。
我不知道列表是否不会自动更新,或者列表在项目添加到MoodViewModel类中的moodArray之前先加载。
谢谢。
更新:
在我的FirstView上
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类上
我添加了.navigationBarItems(trailing: NavigationLink(destination:MyMoodList(currentMood: currentMood, addNewPost: true))
@State var addNewPost: Bool
答案 0 :(得分:0)
在onAppear
结构的NavigationView
上使用。MyMoodList
修饰符将字符串添加到数组中。
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)
}
}
}}