this is the code
struct ContentView: View {
@State var path = NavigationPath()
var body: some View {
NavigationStack(path: $path){
List{
NavigationLink {
View1(){
print(path.count)
print(path)
}
} label: {
Text("level0 ")
}
}
}
}
}
struct View1: View {
var go: () -> Void
var body: some View {
List{
NavigationLink {
View2(){
go()
print("went to view2")
}
} label: {
Text("level 1 ")
}
}
.onAppear(){
print("view1 enter")
go()
}
}
}
struct View2: View {
var go: ()-> Void
var body: some View {
List{
NavigationLink {
View3(){
go()
print("went to view3")
}
} label: {
Text("level 2 ")
}
}
.onAppear(){
print("view2 enter")
go()
}
}
}
struct View3: View {
var go: ()-> Void
var body: some View {
List{
Text("level 3")
.onAppear(){
print("view3 enter")
go()
}
}
}
}
and this is the print log
view1 enter
0
NavigationPath(_items: SwiftUI.NavigationPath.(unknown context at $1c5c2c248).Representation.eager([]), subsequentItems: [], iterationIndex: 0)
view2 enter
0
NavigationPath(_items: SwiftUI.NavigationPath.(unknown context at $1c5c2c248).Representation.eager([]), subsequentItems: [], iterationIndex: 0)
went to view2
view3 enter
0
NavigationPath(_items: SwiftUI.NavigationPath.(unknown context at $1c5c2c248).Representation.eager([]), subsequentItems: [], iterationIndex: 0)
went to view2
went to view3
question
NavigationPath not updating with NavigationStack,
when I open view1 and view2 ,why NavigationPath not update?
Post
Replies
Boosts
Views
Activity
the Model Code
import Foundation
import SwiftData
@Model
class TestItemTotal {
var id = UUID()
var name: String
var describe: String
init(name: String = "", describe: String = "") {
self.name = name
self.describe = describe
}
}
the contentView code
import SwiftUI
class ShareData: ObservableObject {
@Published var path: NavigationPath = NavigationPath()
@Published var selectedTestItemTotal = TestItemTotal()
}
struct ContentView: View {
@StateObject var shareData = ShareData()
var body: some View {
NavigationStack(path: $shareData.path) {
List{
Text("The Main Project List")
Text("The Detail List")
}
}
}
}
#Preview {
ContentView()
}
Crash
it will crash ,when #preview use Xcode ,
but it will not crash when run in Simulator .
fix Crash
Remove @Model string , it will not crash , when #preview use Xcode ,
but i can't remove @Model String , because i will to processing data by SwiftData
this is my code
import SwiftUI
import SwiftData
@Model
class TestItemTotal {
var name: String
init(_ name: String) {
self.name = name
}
}
struct TestItemTotalDetail: View {
var currentItem: TestItemTotal
var body: some View {
VStack {
Text(currentItem.name)
}
}
init(currentItem: TestItemTotal) {
self.currentItem = currentItem
}
}
#Preview {
var item = TestItemTotal("james qq")
return TestItemTotalDetail(currentItem: item)
}
Crash
it will crash ,when #preview use Xcode
fix Crash
Remove @Model string , it will not crash , when #preview use Xcode ,
but i can't remove @Model String , because i will to processing data by SwiftData
the crash info
crash log
I don't know how to fix this crash .