0

【总结问题】

.onAppear.navigationViewStyle(.stack)在 NavigationView 上使用时调用了两次修饰符。

[描述预期和实际结果]

我期望.onAppear被调用一次,但.onAppear被调用两次。

编辑:我删除了项目的链接并添加了一个更简单的模板代码来显示问题。

import SwiftUI

struct RootView: View {
    
    @State private var showDestinationView = false
    
    var body: some View {
        NavigationView {
            LeftView()
            
            NavigationView {
                NavigationLink(isActive: $showDestinationView) {
                    DestinationView()
                } label: {
                    Button("Show Destination") {
                        print("didPressButton")
                        showDestinationView = true
                    }
                }
            }
            .navigationBarTitle("Title")
            .navigationViewStyle(.stack) // Without this line, onAppear is called once.
        }
    }
}

struct DestinationView: View {
    var body: some View {
        Text("Destination View")
            .onAppear {
                print("onAppear")
            }
    }
}

struct LeftView: View {
    var body: some View {
        VStack {
            Spacer()
            
            Text("Left")
            
            Spacer()
        }
        
    }
}

这是日志:

didPressButton
onAppear
onAppear
4

0 回答 0