Learn Courses My Dashboard

datePicker Problems

I’m having problems with being able to change a date on a view. The data is stored in CoreData

On one view, PersonAddNew, I have a datePicker to select the person’s birthdate, and it works fine.

On another view, PersonView, when I put in a datePicker, the view does not display at all. When I comment out the datePicker the view displays normally. Below is the code with the datePicker not commented out.

@State var title: String = ""
@State var firstName: String = ""
@State var middleName: String = ""
@State var lastName: String = ""
@State var birthDate = Date()

    GeometryReader { geo in
        //MARK: Enter view
        Form{
            Section(header: HStack {
                Button(action: cancelData) {
                    Text("Cancel")}
                Spacer()
                Button(action: setEditMode) {
                    Text(editOrSave)}
                Spacer()
                Button(action: deletePerson) {
                    Text("Delete")}
                .disabled(isDisabled)
            })
            {
                VStack(alignment: .leading){
                    Text("Person")
                        .font(.headline)
                    TextField(oldPerson.title ?? "Enter Title", text: $title)
                    TextField(oldPerson.firstName ?? "Enter First Name", text: $firstName)
                    TextField(oldPerson.middleName ?? "Enter Middle Name", text: $middleName)
                    TextField(oldPerson.lastName ?? "Enter Last Name", text: $lastName)
                    DatePicker("", selection: $birthDate, displayedComponents: .date)
                        .datePickerStyle(.wheel)
                }
            }
        }
    }
    .environmentObject(Person())

Take out the VStack. You don’t need that since all the items are inside a Form.

It worked, thanks.