如何在 Xcode Mac Os App 中多次浏览视图

How to navigate through views more than once in Xcode Mac Os App

我正在 Xcode 中创建一个小的 macOs 应用程序。我想多次浏览视图。当我浏览一个视图并单击转到另一个视图时,它不起作用,代码如下:

'''
struct ContentView: View {
    @Environment(\.managedObjectContext) private var viewContext

    @FetchRequest(
        sortDescriptors: [], animation: .default) private var postits: FetchedResults<Postit>

    var body: some View {
        NavigationView {
            ScrollView {
            VStack {
                
                NavigationLink(destination: CreatePostitView()) {
                Image(systemName: "plus")
                } 
                // for example if i navigate to this view and then navigate to the a 
                // postit view it doesn't work, or viceversa.
                
                ForEach(postits) {postit in
                    NavigationLink(destination: PostitView(title: postit.title! , 
                    content: postit.content!)) {
                        Text(postit.title!)
                    } // for example if i navigate to this view and then navigate to 
                      //the create postit view it doesn't work, or viceversa.

                    .frame(width: 100, height: 100)
                    .background(Color.yellow .opacity(0.3))
                    .shadow(color: Color.yellow, radius: 10)
                    .padding(16)
                }
            }
        }
    }
    }
    
    '''

这是 postit 视图结构,以备不时之需

'''
struct CreatePostitView: View {
    var body: some View {
        Text("Creaate postit view")
    }
}
'''

这是便利贴的详细视图,以备不时之需

'''
struct PostitView: View {
    
    let title : String
    let content : String
    
    var body: some View {
        Text(title)
            .font(.largeTitle)
            .padding()
        
        Text(content)
            .font(.title2)
    }
}
'''

您应该在 List

内的边栏中添加您想要的所有内容
NavigationView {
    List {
        NavigationLink(destination: CreatePostitView()) {
            Image(systemName: "plus")
        }

        ForEach(postits) {postit in
            NavigationLink(destination: PostitView(title: postit.title ,
                                                   content: postit.content)) {
                Text(postit.title)
            }

        }
    }
    .listStyle(.sidebar)
}

(为简洁起见,我删除了一些 UI 代码)