Type {
// Title
TextField("Title", textual content: $viewModel.title).textFieldStyle(DefaultTextFieldStyle())
//DAte
DatePicker("Due Date", choice: $viewModel.dueDate)
.datePickerStyle(CompactDatePickerStyle()).padding(.all, 10)
//Button
TLButton(textual content: "Add to Do") {
if (viewModel.canSave) {
viewModel.save()
newItemPresented = false;
} else {
viewModel.showAlert = true
print(viewModel.showAlert)
}
}
}
.alert(isPresented: $viewModel.showAlert) {
Alert(title: Textual content("Error"),
message: Textual content("Enter Accurately")
)
}
I’m making an attempt to make an alert popup when the end-user provides incorrect particulars on the shape, which is validated by an exterior class.
I’ve declared the involved variables within the exterior class as follows
@Printed var title = ""
@Printed var dueDate = Date()
@Printed var showAlert = false
I’m additionally attaching all related features (for my part) to the scope of this drawback:
var canSave: Bool {
guard !title.trimmingCharacters(in: .whitespaces).isEmpty else {
return false;
}
guard dueDate >= Date().addingTimeInterval(-86400) else {
return false;
}
return true
}
My TLButton Class:
import SwiftUI
struct TLButton: View {
let textual content: String
let motion: () -> Void
var physique: some View {
Button {
//Try log in
motion()
} label: {
ZStack {
RoundedRectangle(cornerRadius: 10).foregroundColor(.blue)
Textual content(textual content).foregroundColor(.white)
}
}.padding(.backside, 5)
}
}
#Preview {
TLButton(textual content: "Foo") {
// motion
}
}
i attempted printing the worth of viewModel.showAlert
to see if it updates it to true, when it’s initially set to false. It does replace it, nevertheless, the alert doesn’t come. I count on the Alert to pop up with the related picture.