在下面的代码中,按照提示
Click to select entry
-> Click to populate linkedUuid
,顶部屏幕将关闭并返回到 Home
屏幕。由于 dismiss()
在 ShowAccounts
视图中被调用,我认为只有 ShowAccounts
或 SelectEntry
被忽略,而 Text("NavDest: Text-1")
完好无损。
代码的行为不符合我的预期。我在这里误解了什么?谢谢你。
struct SO1_GAUsername: View {
let SRC = "SO1_GAUsername"
//=== Local ===
@State private var linkedUuid: String? = "<???>"
var body: some View {
Text(SRC)
HStack {
Text("LinkEntry")
Text(linkedUuid!)
.foregroundColor(.yellow)
}
LinkEntry(linkedUuid: $linkedUuid)
} // body
struct LinkEntry: View {
let SRC = "LinkEntry"
//=== Interface ===
@Binding var linkedUuid: String?
//=== Local ===
@Environment(\.presentationMode) @Binding private var presentationMode
@State private var showSelectEntry = false
var body: some View {
let _ = print("\(SRC): Called")
Button("Click to select entry") {
showSelectEntry = true
}
.foregroundColor(.blue)
.navigationDestination(isPresented: $showSelectEntry) {
Text("NavDest: Text-1")
SelectEntry(linkedUuid: $linkedUuid)
} // navDest
} // body
} // LinkEntry
struct SelectEntry: View {
let SRC = "SelectEntry"
//=== Interface ===
@Binding var linkedUuid: String?
//=== Local ===
@Environment(\.presentationMode) @Binding private var presentationMode
var body: some View {
let _ = print("\(SRC): Called")
ShowAccounts(linkedUuid: $linkedUuid)
} // body
} // SelectEntry
struct ShowAccounts: View {
let SRC = "ShowAccounts"
//=== Interface ===
@Binding var linkedUuid: String?
//=== Local ===
@Environment(\.presentationMode) @Binding private var presentationMode
var body: some View {
let _ = print("\(SRC): Called")
Button("\(SRC): Click to populate linkedUuid") {
let SRC = self.SRC + ".Button.click"
linkedUuid = "123xyz"
print("\(SRC): Dismissing")
presentationMode.dismiss()
}
.foregroundColor(.blue)
} // body
} // ShowAccounts
} // SO1_GAUsername
根据 Apple 文档,
dismiss
可用于:
也就是说,
dismiss()
删除整个包含视图(屏幕),而不仅仅是一个包含视图。因此,上述行为是正确的。