I have a SwiftUI view that looks like this (simplified for privacy reasons):
var body: some View {
HStack {
Button(action: {
self.completed.toggle()
}) {
Text(self.title)
}
NavigationLink(destination: DetailView(task: self.task)) {
Text(self.title)
}
}
}
The navigation works just fine, and the DetailView
has the updated status (toggle
just toggles the status), however when I go back to the main view, the status is not updated. Why is this?
And I need the button to act differently to the text - is this possible? I want the button press to perform the button action, and the rest of the row to act as the NavigationLink
. How can I do this?
The self.completed
is a CompletionStatus
class that is an ObservableObject
, with a @Published
property status
. task
is @State
of type Task
, which contains the completed
(CompletionStatus) property. Please let me know if I can provide any more information.
Here are the Task
and CompletionStatus
structs and classes respectively:
struct Task: Identifiable {
let id: UUID()
var title: String
var description: String
@ObservedObject var completed: CompletionStatus
}
class CompletionStatus: ObservableObject {
@Published var status: Bool
init(_ defaultValue: Bool) {
self.status = defaultValue
}
func toggle() -> CompletionStatus {
self.status = !self.status
return self
}
}