当被 navDestination 的视图调用时,dismiss() 的预期行为是什么?

问题描述 投票:0回答:1

在下面的代码中,按照提示

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


swiftui dismiss
1个回答
0
投票

根据 Apple 文档

dismiss
可用于:

  • 关闭模态演示,例如工作表或弹出框。
  • 从 NavigationStack 中弹出当前视图。

也就是说,

dismiss()
删除整个包含视图(屏幕),而不仅仅是一个包含视图。因此,上述行为是正确的。

© www.soinside.com 2019 - 2024. All rights reserved.