0

我正在尝试以 swiftui 的形式实现日期选择器。当用户选择它时,它应该显示生日。在这里你可以找到我的大部分代码。我不知道该怎么做,请帮忙。

非常感谢你。

import SwiftUI


struct DetailView: View {
    
    var dateFormatter: DateFormatter {
        let formatter = DateFormatter()
        formatter.dateStyle = .long
        return formatter
    }
    
    @ObservedObject var dm : DataManager
    var bimba : ContactModel
    
    
    @State var isSharePresented = false
    @State var isAlertPresented = false
    @State var isAddPresented = false
    @State var nome : String = ""
    @State var birthDate = Date()
    
    
    
    var items : [Any] {
        return [bimba.realImage, bimba.nome]
    }
    
    var body: some View {
        ZStack {
          //  Color.blue
            VStack {
                VStack {
                    HStack {
                        VStack {
                            Spacer()
                                .padding()
                            Form {
                                
                                Section {
                                    
                                    HStack {
                                        Text(loc("RAZ"))

                                        Spacer()
                                        Text(bimba.razza)
                                            .foregroundColor(.gray)
                                            .font(.callout)
                                    }
                                    
                                    HStack {
                                        Text("Sesso")

                                        Spacer()
                                        Text(bimba.gender)
                                            .foregroundColor(.gray)
                                            .font(.callout)
                                
                                    }
                                    HStack {
                                        Text("Peso")

                                        Spacer()
                                        Text(bimba.peso)
                                            .foregroundColor(.gray)
                                            .font(.callout)
                                        Text("kg")
                                            .foregroundColor(.gray)
                                    }
                                    HStack {
                                        Text("Età")
                                        Spacer()
                                        Text(bimba.bday)
                                            .foregroundColor(.gray)
                                            .font(.callout)
                                        Text("Anni")
                                            .foregroundColor(.gray)
                                    }
                                    
                                    DatePicker("Birthdate", selection: $birthDate, in: ...Date(), displayedComponents: .date)
                                        .accentColor(Color.gray)
                                    
                                }
                                
                                Section {
                                    
                                    HStack {
                                        Text("Mantello")
                                        Spacer()
                                        Text(bimba.mantello)
                                            .foregroundColor(.gray)
                                            .font(.callout)
                                    }
                                    HStack {
                                        Text("Colore")
                                        Spacer()
                                        Text(bimba.colore)
                                            .foregroundColor(.gray)
                                            .font(.callout)
                                    }
                                    HStack {
                                        Text("Pelo")
                                        Spacer()
                                        Text(bimba.pelo)
                                            .foregroundColor(.gray)
                                            .font(.callout)
                                    }
                                    HStack {
                                        Text("Orecchie")
                                        Spacer()
                                        Text(bimba.orecchie)
                                            .foregroundColor(.gray)
                                            .font(.callout)
                                    }
                                    HStack {
                                        Text("Coda")
                                        Spacer()
                                        Text(bimba.coda)
                                            .foregroundColor(.gray)
                                            .font(.callout)
                                    }
                                    
                                    
                                }
                                
                                Section {
                                    HStack {
                                        Text("Chip No")
                                        
                                        Spacer()
                                        Text(bimba.chip)
                                            .foregroundColor(.gray)
                                            .font(.callout)
                                    }
                                    HStack {
                                        Text("Nome Pedigree")
                                        
                                        Spacer()
                                        Text(bimba.nomePedig)
                                            .foregroundColor(.gray)
                                            .font(.callout)
                                    }
                                    HStack {
                                        Text("Codice ENCI")
                                        
                                        Spacer()
                                        Text(bimba.codiceEnci)
                                            .foregroundColor(.gray)
                                            .font(.callout)
                                    }
                                    HStack {
                                        Text("Data Registrazione")
                                        
                                        Spacer()
                                        Text(bimba.regisDate)
                                            .foregroundColor(.gray)
                                            .font(.callout)
                                    }
                                    HStack {
                                        Text("ID No")
                                        
                                        Spacer()
                                        Text(bimba.codiceId)
                                            .foregroundColor(.gray)
                                            .font(.callout)
                                    }

                                }
                                Section {
                                    HStack {
                                        Text("Altro")
                                        
                                        Spacer()
                                        Text(bimba.altroInfo)
                                            .foregroundColor(.gray)
                                            .font(.callout)
                                    }
                                }

                            }
                            Spacer()
                        }
                    }.padding(.bottom, -80)
                }.background(
                    Text(bimba.nome)
                        .font(.largeTitle)
                        .fontWeight(.semibold)
                        .offset(x: 0, y: -50))
                
                .background(
                    Text(bimba.nick)
                        .font(.subheadline)
                        .fontWeight(.semibold)
                    //       .offset(x: 0, y: -50))
                )
                .offset(x: 0, y: -80)
                
                .background(
                    Image(uiImage: bimba.realImage)
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                        .frame(width: 180, height: 180)
                        .clipShape(Circle())
                        .offset(x: 0, y: 0)
                        .shadow(radius: 6)
                        .padding(.bottom, 530))
                Spacer()
                
                // .navigationBarTitle(Text(bimba.nome), displayMode: .large)
                VStack {
                    HStack {
                        editButton.padding(.horizontal, 16).offset(x: 0, y: -5)
                        Spacer()
                        shareButton.padding(.horizontal, 16).offset(x: 0, y: -5)
                    }.frame(height: 44)
                }
                .frame(height: 34)
                
            }
        }
        //  .navigationBarTitle(Text(bimba.nome), displayMode: .large)
    }
4

2 回答 2

0

我将您的代码复制到我的示例项目中,并且 DatePicker 工作正常 - 在第一个屏幕上,我点击日期为 29.04.2020 的日期视图,它向我显示 DatePicker 视图(第二个屏幕)的日期正确,然后我将日期更改为29.01.2021(第三屏)和 DatePicker 视图关闭后日期正确(第四屏)。问题出在哪里?也许我错过了什么

在此处输入图像描述

于 2021-03-19T23:45:20.720 回答
0

当您将生日对象创建为 Date() 时,它会在每次 DetailView 重新呈现时分配确切的日期和时间。如果您将用户的生日存储在任何地方,则需要在表单中调用该信息。

编辑:

var birthdayOfUser = Date().addingTimeInterval(24.0 * 3600) // you add time intervals as seconds

DatePicker("Birthdate", selection: $birthdayOfUser, in: ...Date(), displayedComponents: .date)
.accentColor(Color.gray)

此示例将明天绑定为 DatePicker 将显示的日期。您需要做的就是像这样绑定存储的用户生日。

顺便说一下,这个生日示例是未来的,并且无效。我只是想显示一个不同的 Date 对象,而不是确切的时间。

于 2021-03-20T05:38:57.463 回答