[ad_1]
I’ve a navigation hyperlink in a ForEach loop that navigates to the settings of that merchandise. Inside that listing merchandise, I’ve some settings that are being saved into CoreData as an array of ints.
Once I save the information contained in the element view and return to the primary listing stack, the information would not replace from the saved modifications. Nonetheless, going into the element view and again out updates it.
Here is what’s taking place in the mean time:
I begin off with an empty textual content space, then go into the merchandise, add some information (what’s saved onDismiss), and the textual content would not replace. Going again in after which out the numbers seem on the underside.
A stripped down model of the code I am utilizing:
Record view
NavigationView {
ForEach(favourites) { favorite in // favourites is from CoreData
NavigationLink {
DetailView(favorite: favorite)
} label: {
FavouriteCell(favorite: favorite)
}
}
}
FavouriteCell
let favorite: Favorite
@State non-public var days: String = ""
var physique: some View {
VStack {
HStack {
Textual content("Take a look at information")
Textual content("Take a look at information")
}
Textual content(String(days.sorted()))
.onAppear { days = getDays(from: favorite.notificationDays) }
}
}
// rudimentary perform for testing
func getDays(from selectedDays: [Int]?) -> String {
guard let selectedDays = selectedDays else { return "" }
var output = ""
for day in selectedDays {
output += "(day)"
}
return output
}
DetailView
@State non-public var showAlerts: Bool = false
let favorite: Favorite
var physique: some View {
Part {
Toggle("Get alerts", isOn: $showAlerts)
if(showAlerts) {
HStack(spacing: 10) {
ForEach(0..<7) { day in
Textual content("(day)")
.onTapGesture { checkActiveDays(day.index) }
}
}
}
}
.onDisappear { coreDM().replace() }
func checkActiveDays(_ index: Int) {
if( selectedDays.accommodates(index) ) {
selectedDays.take away(at: selectedDays.firstIndex(of: index)!)
} else {
selectedDays.append(index)
}
}
}
[ad_2]
