I have a custom "Homework" class that has an "isComplete" boolean value. It is displayed on a custom view called HomeworkView.
HomeworkView has a subview called ToggleView which contains a label and toggle to show, you guessed it, whether the homework is complete or not. I am not sure how to set this up to where the toggle in ToggleView changes the value of the "homework" instance. Any help for this noob is appreciated.
My class:-
class Homework {
var title: String
var details: String
var isComplete: Bool
init(title: String, details: String, isComplete: Bool) {
self.title = title
self.details = details
self.isComplete = isComplete
}
}
ToggleView and HomeworkView:-
struct ToggleView: View {
@State var isComplete: Bool
var body: some View {
HStack {
Text("Is it complete?")
.labelsHidden()
VStack(alignment: .center) {
Text(isComplete ? "YES" : "NO")
.foregroundColor(isComplete ? .green : .orange)
Toggle("", isOn: $isComplete)
}
.padding(.horizontal)
.frame(width: 80)
}
.padding()
}
}
struct HomeworkView: View {
@State var homework: Homework
var body: some View {
VStack {
Text(homework.isComplete ? "COMPLETED" : "PENDING")
.font(.largeTitle)
ToggleView(isComplete: homework.isComplete)
.onTapGesture {
print("\(homework.isComplete.hashValue)")
}
}
}
}