我有一个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)
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。