I have a ForEach loop for my StudentStore
and only want to display them in my list if the isFavorite
field is true.
The problem is when I edit the value of isFavorite inside SecondView
, it goes back to ContentView
because of the if
statement inside ContentView
.
I want the list to update when I get back to ContentView
rather than updating it immediately causing it to go back to the original page.
When I was looking for a solution I came upon this:
Read this entire thread thinking you were asking if you could make a NavigationView conditionally hidden and thought, “That sounds like a mess...”
Source: https://www.reddit.com/r/SwiftUI/comments/e2wn09/make_navigationlink_conditional_based_on_bool/
Can someone also how to solve this problem and why conditionally hiding a NavigationView
is a bad idea?
Model
import SwiftUI
struct StudentItem: Identifiable {
var id = UUID()
var name: String
var isFavorite: Bool
init(name: String, isFavorite: Bool) {
self.name = name
self.isFavorite = isFavorite
}
}
class StudentStore: ObservableObject {
@Published var StudentStore: [StudentItem]
init(StudentStore: [StudentItem]){
self.StudentStore = StudentStore
}
}
Main view
struct ContentView: View {
@ObservedObject var studentStore = StudentStore(StudentStore: [StudentItem(name: "Stephen", isFavorite: true),
StudentItem(name: "Jay", isFavorite: true),
StudentItem(name: "Isaac", isFavorite: true),
StudentItem(name: "Talha", isFavorite: true),
StudentItem(name: "John", isFavorite: true),
StudentItem(name: "Matt", isFavorite: true),
StudentItem(name: "Leo", isFavorite: true)])
var body: some View {
NavigationView {
List {
ForEach(studentStore.StudentStore.indices, id: \.self) { index in
Group {
if self.studentStore.StudentStore[index].isFavorite == true {
NavigationLink(destination: SecondView(student: self.$studentStore.StudentStore[index])) {
HStack {
Text(self.studentStore.StudentStore[index].name)
Image(systemName: self.studentStore.StudentStore[index].isFavorite ? "star.fill" : "star")
}
}
}
}
}
}
}
}
}
Sub view
struct SecondView: View {
@Binding var student: StudentItem
var body: some View {
Button(action: {
self.student.isFavorite.toggle()
}) {
Image(systemName: student.isFavorite ? "star.fill" : "star")
}
}
}