Thanks for answering szymczyk I will try to answer your points:
- The upgrade is no relevant as I've started from scratch
- In XCode 11.4.1... New->Project->macOS->swiftUI & document-based application & use core data
- For the last two points, I include the code...
// Documents.swift
override func makeWindowControllers() {
// Create the SwiftUI view and set the context as the value for the managedObjectContext environment keyPath.
// Add `@Environment(\.managedObjectContext)` in the views that will need the context.
let contentView = ContentView().environment(\.managedObjectContext, self.managedObjectContext!)
// Create the window and set the content view.
let window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 1050, height: 660),
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered, defer: false)
window.center()
window.contentView = NSHostingView(rootView: contentView)
window.makeKeyAndOrderFront(nil)
let windowController = NSWindowController(window: window)
self.addWindowController(windowController)
}
// ContentView.swift
struct ContentView: View {
@State private var selectedItem: Item?
var body: some View {
NavigationView {
NavigationMaster(item: $selectedItem)
if selectedItem != nil {
NavigationDetail(item: $selectedItem) // The DrescriptionView is called from here
}
else {
Text("Please select an item from the list").frame(maxWidth: .infinity, maxHeight: .infinity)
}
}.frame(minWidth: 800, minHeight: 400)
}
// DescriptionView.swift
// The Text field is taken from the Parameters obect in core data... One record (name == "Name") is fetched
// updated when the moc is save
struct DescriptionView: View {
@Environment(\.managedObjectContext) var moc
@FetchRequest(
entity: Parameter.entity(),
sortDescriptors: [ NSSortDescriptor(keyPath: \Parameter.name, ascending: true),],
predicate: NSPredicate(format: "name == %@", "Name")
) var parameter_name: FetchedResults<Parameter>
@State private var name : String = ""
var body: some View {
Form{
// Content
VStack(alignment: .leading) {
// Additional VStack to ensure is centrally aligned and the max width is 750
VStack(alignment: .leading, spacing: 10) {
// Model section
Section(header: Text("Model").font(.headline).fontWeight(.light)) {
HStack {
Text("Name:").frame(width: 100.0, alignment: .leading)
TextField("", text:$name).frame(width: 300.0)
}
).frame(width: 204.0)
}
}.frame(width: 750, alignment: .leading)
.padding(.top, 10)
}
}.padding(20.0)
.frame( maxHeight: .infinity, alignment: .top )
.onAppear{
self.name = self.parameter_name[0].valueString
}
.onDisappear{
self.parameter_name[0].valueString = self.name
}
}
}
Please let me know if you need something else