开发者社区> 问答> 正文

无法从核心数据加载SwiftUI选取器选择并显示为选择

我有一个SwiftUI应用程序,可以将数据保存在核心数据中。我希望有一个选择器,用户可以在其中选择一个项目(从一个实体),然后将该信息存储在另一个实体中,作为选择器显示。

我有一个名为Patient的实体,用户在其中添加数据。我有一个名为诊所的实体,它是用户创建的对象列表。其目的是使用选择器快速添加信息,而不重复地为每个患者记录键入项目。

在SWIFT中,我通过将UIPicker附加到TextField来做到这一点。效果很好。我无法在SwiftUI中完成相应的操作。我可以创建和填充选择器数据,并成功地将更改保存到患者实体中,但在加载详细信息视图时,我无法将患者诊所数据作为默认数据加载到选择器中。

该应用程序的这一部分相当于主/详细样式。DetailView是:

struct DetailView: View {

    @Environment(\.managedObjectContext) var managedObjectContext
    @Environment(\.presentationMode) var presentationMode
    @FetchRequest(fetchRequest: Clinic.getAllClinics()) var clinics: FetchedResults<Clinic>
    @State private var selectClinicItem: Int = 0
    var patient: Patient

    @State private var updatedFirstName: String = "No First Name"
    @State private var updatedLastName: String = "No Last Name"
    @State private var updatedClinic: String = "Clinic Not Chosen"
    @State private var chosenClinicUUID: UUID = UUID()

    var body: some View {

        ScrollView {
            Group {//top group
                VStack {
                    MyDataCell(tfData: $updatedFirstName, passedInLabel: "First Name", patient: patient, patientAttribute: patient.firstName ?? "No First")
                }//Top VStack - 8 views
            }//top Group

            Group {//second group
                Form {
                    Picker(selection: self.$chosenClinicUUID, label: Text("Choose Clinic")) {
                        ForEach(clinics) { c in
                            Text("\(c.name ?? "no name")")
                        }
                    }
                    .frame(width:320, height: 30)
                }//Form
                .frame(width:350, height: 200)
                .clipShape(RoundedRectangle(cornerRadius: 12))
                .overlay(RoundedRectangle(cornerRadius: 12)
                .stroke(Color.gray, lineWidth: 2))
                .padding(.leading, 12)
                .padding(.top, 20)
                .padding(.bottom, 20)
            }//second Group

        }//scroll or top level Form

            .modifier(AdaptsToSoftwareKeyboard())
            .navigationBarTitle("View and Edit", displayMode: .inline)
            .navigationBarItems(trailing: Button(action: {

                self.patient.firstName = self.updatedFirstName
                self.patient.lastName = self.updatedLastName
                self.patient.clinicName = self.updatedClinic
                self.patient.clinicID = self.chosenClinicUUID

                let n = self.clinics.filter { $0.id == self.patient.clinicID }

                if n.count > 0 {
                    self.patient.clinicName = n[0].name
                }

                do {
                    try self.managedObjectContext.save()
                } catch {
                    print(error)
                }

                self.updatedFirstName = ""
                self.updatedLastName = ""
                self.updatedClinic = ""

                self.presentationMode.wrappedValue.dismiss()

            }) {
                Text("Save")
            })

    }//body
}

作为参考,病人阶层:

public class Patient : NSManagedObject, Identifiable {
    @NSManaged public var id: UUID
    @NSManaged public var firstName: String?
    @NSManaged public var lastName: String?
    @NSManaged public var clinicName: String?
    @NSManaged public var clinicID: UUID
}

extension Patient {
    static func getAllPatients() -> NSFetchRequest<Patient> {
        let request: NSFetchRequest<Patient> = Patient.fetchRequest() as! NSFetchRequest<Patient>
        let sortDescriptor = NSSortDescriptor(key: "lastName", ascending: true)
        request.sortDescriptors = [sortDescriptor]
        return request
    }
}

诊所课程:

public class Clinic : NSManagedObject, Identifiable {
    @NSManaged public var id: UUID
    @NSManaged public var name: String?
    @NSManaged public var comment: String?
    @NSManaged public var isShown: Bool
}

extension Clinic {
    static func getAllClinics() -> NSFetchRequest<Clinic> {
        let request: NSFetchRequest<Clinic> = Clinic.fetchRequest() as! NSFetchRequest<Clinic>
        let sortDescriptor = NSSortDescriptor(key: "name", ascending: true)
        request.sortDescriptors = [sortDescriptor]
        return request
    }
}

https://i.stack.imgur.com/cCXnp.png

https://i.stack.imgur.com/UU0O5.png 如有任何指导,将不胜感激。Xcode版本11.2.1(11B500)

展开
收起
游客5akardh5cojhg 2019-12-09 15:05:44 503 0
0 条回答
写回答
取消 提交回答
问答地址:
问答排行榜
最热
最新

相关电子书

更多
4个迭代,从批量交...1573957773.pdf 立即下载
动态、高效,蚂蚁动态卡片的内核逻辑 立即下载
用计算和数据去改变整个世界 立即下载