我正在 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")
}
}
'''
這是 postit 詳細視圖,以防您也需要它
'''
struct PostitView: View {
let title : String
let content : String
var body: some View {
Text(title)
.font(.largeTitle)
.padding()
Text(content)
.font(.title2)
}
}
'''
uj5u.com熱心網友回復:
您應該在側邊欄中添加您想要的所有內容 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 代碼)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/349198.html
