I am new to Swift UI & trying to figure out how to programmatically open a sub view of mine (which contains a nav stack) after my function completes & returns the CKRecord to be displayed.
However, I’ve found that by adding a NavigationStack to the sub view, it causes it to immediately dismiss when it is presented.
See below:
struct ContentView: View {
@State private var path = NavigationPath()
var body: some View {
NavigationStack (path: $path) {
Button(action: {
getExampleRecord { exampleRecord in
path.append(exampleRecord!)
}
}, label: {
Text("Open Record")
}).navigationDestination(for: CKRecord.self) { record in
RecordDisplay(recordToShow: record)
}
}
}
}
struct RecordDisplay: View {
var recordToShow: CKRecord?
var body: some View {
NavigationStack { // Remove this and the view will display fine.
Text("Hello World!")
Text("RecordID: (recordToShow!.recordID.recordName)")
}
}
}
func getExampleRecord (completion: @escaping (CKRecord?) -> Void) {
let recordID = CKRecord.ID(recordName: "example")
let exampleRecord = CKRecord(recordType: "ExampleType", recordID: recordID)
completion(exampleRecord)
}
My subview (RecordDisplay) needs to have NavigationStack, as it itself may be presenting subviews as well as the view might be shown and loaded in other capacities, so I can’t simply remove NavigationStack from RecordDisplay to resolve this issue.
Is there a better or different way to present views after I’ve completed the loading of my CKRecord that would resolve this issue?