I am creating a little macOs app in Xcode. And i want to navigate through views more than one time. When I navigate through one view and click to go to another it doesn't work, here's the code:
'''
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)
}
}
}
}
}
'''
Here is the postit view structure in case you need it
'''
struct CreatePostitView: View {
var body: some View {
Text("Creaate postit view")
}
}
'''
And here is the postit detail view in case you need it too
'''
struct PostitView: View {
let title : String
let content : String
var body: some View {
Text(title)
.font(.largeTitle)
.padding()
Text(content)
.font(.title2)
}
}
'''
CodePudding user response:
You should add everything you want in the sidebar inside a 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)
}
(I removed some UI code for brevity)