Ios 如何使用SwiftUI在详图视图上编辑和覆盖CoreData
我正在开发一个患者管理系统,在这个系统中,我需要输入患者的所有相关信息,并在需要时列出、保存和编辑这些信息。我已经设法保存它们,列出它们,并在进入“详细信息”视图时显示它们,但我无法编辑它们并将新信息保存在旧信息之上 我已经研究了所有可能解决这个问题的答案,最近的一次是创建了一个重复的患者文件 这是我的代码(属性是法语) PatientList文件:Ios 如何使用SwiftUI在详图视图上编辑和覆盖CoreData,ios,swiftui,swiftui-list,Ios,Swiftui,Swiftui List,我正在开发一个患者管理系统,在这个系统中,我需要输入患者的所有相关信息,并在需要时列出、保存和编辑这些信息。我已经设法保存它们,列出它们,并在进入“详细信息”视图时显示它们,但我无法编辑它们并将新信息保存在旧信息之上 我已经研究了所有可能解决这个问题的答案,最近的一次是创建了一个重复的患者文件 这是我的代码(属性是法语) PatientList文件: struct PatientList: View { @Environment(\.managedObjectContext) var
struct PatientList: View {
@Environment(\.managedObjectContext) var moc
@FetchRequest(entity: PatientInfo.entity(), sortDescriptors: [
NSSortDescriptor(keyPath: \PatientInfo.nom, ascending: true),
NSSortDescriptor(keyPath: \PatientInfo.prenom, ascending: true)])
var patients: FetchedResults<PatientInfo>
@State private var showingAddScreen = false
var body: some View {
NavigationView {
List {
ForEach(patients, id: \.self) { patient in
NavigationLink(destination: PatientDetail(patients: patient))
{
VStack{
HStack{
Text(patient.nom ?? "Inconnu")
Text(patient.prenom ?? "Inconnu")
}
}
}
}
.onDelete(perform: deletePatients)
}
.navigationBarTitle("Patients")
.navigationBarItems(leading: EditButton(), trailing:
Button(action: {
self.showingAddScreen.toggle()
}) {
Image(systemName: "plus")
}
)
.sheet(isPresented: $showingAddScreen) {
AddNewPatient().environment(\.managedObjectContext, self.moc)
}
}
}
func deletePatients(at offsets: IndexSet) {
for offset in offsets {
let patient = patients[offset]
moc.delete(patient)
}
try? moc.save()
}
struct PatientList_Previews: PreviewProvider {
static var previews: some View {
PatientList()
}
}
}
PatientDetail文件:
struct PatientDetail: View {
@Environment(\.managedObjectContext) var moc
@Binding var patients: PatientInfo
@State private var showingEditScreen = false
var body: some View {
NavigationView{
Form{
Section{
Text(self.patients.nom ?? "Nom")
Text(self.patients.prenom ?? "Prénom")
}
Section{
Text(self.patients.adresse ?? "Adresse")
}
Section{
Text(self.patients.codepostal ?? "Code Postal")
}
Section{
Text(self.patients.ville ?? "Ville")
}
Section{
Text(self.patients.province ?? "Province")
}
Section{
Text(self.patients.numerotelephone ?? "000-000-0000")
}
}
}
.navigationBarTitle(Text(patients.nom ?? "Inconnu"), displayMode: .inline)
.navigationBarItems(trailing: Button(action: { self.showingEditScreen.toggle()
}) {
Image(systemName: "square.and.pencil")
}
)
.sheet(isPresented: $showingEditScreen) {
EditPatientView(patients: self.patients).environment(\.managedObjectContext, self.moc)
}
}
struct PatientDetail_Previews: PreviewProvider {
static let moc = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
static var previews: some View {
let patients = PatientInfo(context: moc)
patients.nom = "Amchou"
patients.prenom = "Hicham"
patients.ddn = Date()
patients.adresse = "7580 Francois Chartrand"
patients.codepostal = "H7A 3Z9"
patients.ville = "Laval"
patients.province = "Québec"
patients.numerotelephone = "000-000-0000"
patients.dernierexamen = Date()
return NavigationView {
PatientDetail(patients: PatientInfo)
}
}
}
}
以及EditPatientView文件:
struct EditPatientView: View {
@Environment(\.presentationMode) var presentationMode
@Environment(\.managedObjectContext) var moc
@ObservedObject var patients : PatientInfo
@State private var nom = ""
@State private var prenom = ""
@State private var ddn = Date()
@State private var adresse = ""
@State private var codepostal = ""
@State private var ville = ""
@State private var province = ""
@State private var numerotelephone = "000-000-0000"
@State private var dernierexamen = Date()
var body: some View {
NavigationView {
Form {
Section {
TextField("Nom", text: self.$nom)
TextField("Prénom", text: self.$prenom)
}
Section {
DatePicker("Date de naissance", selection: self.$ddn, displayedComponents: .date)
}
Section {
TextField("Adresse", text: self.$adresse)
TextField("Code Postal", text: self.$codepostal)
TextField("Ville", text: self.$ville)
TextField("Province", text: self.$province)
}
Section {
TextField("Numéro de téléphone", text: self.$numerotelephone).keyboardType(.numberPad)
}
DatePicker("Dernier examen", selection: self.$dernierexamen, displayedComponents: .date)
Section {
Button("Enregistrer") {
let newPatient = PatientInfo(context: self.moc)
newPatient.nom = self.nom
newPatient.prenom = self.prenom
newPatient.ddn = self.ddn
newPatient.adresse = self.adresse
newPatient.codepostal = self.codepostal
newPatient.ville = self.ville
newPatient.province = self.province
newPatient.numerotelephone = self.numerotelephone
newPatient.dernierexamen = self.dernierexamen
try? self.moc.save()
self.presentationMode.wrappedValue.dismiss()
}
}
}
.navigationBarTitle("Patient")
}
}
}
struct EditPatientView_Previews: PreviewProvider {
static var previews: some View {
EditPatientView(patients: PatientInfo())
}
}
(希望这不是太多信息。第一次发布)在EditPatientView中,您可以编辑患者。为什么不编辑它而不是创建一个新患者?比如:
struct editView:视图{
@观察对象变量患者:PatientInfo
@环境(\.managedObjectContext)变量managedObjectContext
var body:一些观点{
形式{
文本字段(“名称”,文本:$patient.Name??)
}
翁迪萨佩尔先生{
try?self.managedObjectContext.save()
}
}
}
问题是TextField绑定可能不是可选的,但核心数据字段是可选的
这个来自的方便扩展实现了以下功能:
func??(左:绑定,右:T)->绑定{
装订(
获取:{lhs.wrappedValue??rhs},
集合:{lhs.wrappedValue=$0}
)
}
在my中还有一个核心数据示例演示了这一点。在EditPatientView中,您可以编辑患者。为什么不编辑它而不是创建一个新患者?比如:
struct editView:视图{
@观察对象变量患者:PatientInfo
@环境(\.managedObjectContext)变量managedObjectContext
var body:一些观点{
形式{
文本字段(“名称”,文本:$patient.Name??)
}
翁迪萨佩尔先生{
try?self.managedObjectContext.save()
}
}
}
问题是TextField绑定可能不是可选的,但核心数据字段是可选的
这个来自的方便扩展实现了以下功能:
func??(左:绑定,右:T)->绑定{
装订(
获取:{lhs.wrappedValue??rhs},
集合:{lhs.wrappedValue=$0}
)
}
在my中还有一个核心数据示例演示了这一点。这完全解决了我的问题。我一直试图理解为什么它不能工作,然后我得到了绑定的东西(很抱歉,这一切都是新的)。通过API,它没有帮助。我感到宽慰。非常感谢!!这完全解决了我的问题。我一直试图理解为什么它不能工作,然后我得到了绑定的东西(很抱歉,这一切都是新的)。通过API,它没有帮助。我感到宽慰。非常感谢!!
struct EditPatientView: View {
@Environment(\.presentationMode) var presentationMode
@Environment(\.managedObjectContext) var moc
@ObservedObject var patients : PatientInfo
@State private var nom = ""
@State private var prenom = ""
@State private var ddn = Date()
@State private var adresse = ""
@State private var codepostal = ""
@State private var ville = ""
@State private var province = ""
@State private var numerotelephone = "000-000-0000"
@State private var dernierexamen = Date()
var body: some View {
NavigationView {
Form {
Section {
TextField("Nom", text: self.$nom)
TextField("Prénom", text: self.$prenom)
}
Section {
DatePicker("Date de naissance", selection: self.$ddn, displayedComponents: .date)
}
Section {
TextField("Adresse", text: self.$adresse)
TextField("Code Postal", text: self.$codepostal)
TextField("Ville", text: self.$ville)
TextField("Province", text: self.$province)
}
Section {
TextField("Numéro de téléphone", text: self.$numerotelephone).keyboardType(.numberPad)
}
DatePicker("Dernier examen", selection: self.$dernierexamen, displayedComponents: .date)
Section {
Button("Enregistrer") {
let newPatient = PatientInfo(context: self.moc)
newPatient.nom = self.nom
newPatient.prenom = self.prenom
newPatient.ddn = self.ddn
newPatient.adresse = self.adresse
newPatient.codepostal = self.codepostal
newPatient.ville = self.ville
newPatient.province = self.province
newPatient.numerotelephone = self.numerotelephone
newPatient.dernierexamen = self.dernierexamen
try? self.moc.save()
self.presentationMode.wrappedValue.dismiss()
}
}
}
.navigationBarTitle("Patient")
}
}
}
struct EditPatientView_Previews: PreviewProvider {
static var previews: some View {
EditPatientView(patients: PatientInfo())
}
}