【问题标题】:How to make date picker display a user specific date (for a birthday for example)如何使日期选择器显示用户特定的日期(例如生日)
【发布时间】:2021-06-17 07:59:02
【问题描述】:

我正在尝试以 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)
    }

【问题讨论】:

    标签: ios swiftui datepicker swiftui-form


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      当您将生日对象创建为 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 对象。

      【讨论】:

      • 添加一些代码 sn-p 以便更好地理解答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-29
      • 2014-01-09
      • 1970-01-01
      • 2020-01-11
      • 2019-03-22
      • 2015-05-01
      • 2012-12-20
      相关资源
      最近更新 更多