我正在关注 Paul Hudson 关于 programmatic navigation 的文章,尽管我遇到了错误。
我使用三个简单的视图重新创建了错误。我想要做的是从 ContentView -> ChildViewOne -> ChildViewTwo -> ContentView 移动。
我从 ChildViewTwo -> ContentView 得到以下错误:
2021-05-20 15:06:44.346713+1200 Test[7756:137785] [Assert] UIScrollView 不支持多个观察者实现 observeScrollView:willEndDraggingWithVelocity:targetContentOffset:unclampedOriginalTarget:。滚动视图 <TtC7SwiftUIP33_BFB370BA5F1BADDC9D83021565761A4925UpdateCoalescingTableView: 0x7fb75a8df000; baseClass = UITableView;帧 = (0 0; 390 844); clipsToBounds = YES;手势识别器 =
返回 ContentView 时我也得到了双重导航。
内容视图
struct ContentView: View {
@State private var isShowingChildOne = false
var body: some View {
NavigationView {
List {
Text("Hello World")
Text("Hello World")
Text("Hello World")
}
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
VStack {
// Programmatic navigation here
NavigationLink(destination: ChildViewOne(), isActive: $isShowingChildOne) { EmptyView() }
Button("Next") {
self.isShowingChildOne = true
}
}
}
}.navigationTitle("ContentView")
}
}
}
ChildViewOne
struct ChildViewOne: View {
var body: some View {
List(0..<5) { item in
NavigationLink(destination: ChildViewTwo()) {
Text("Hello")
}.navigationTitle("Child One")
}
}
}
ChildViewTwo
struct ChildViewTwo: View {
@State private var isShowingContentView = false
var body: some View {
Text("Hello")
.navigationTitle("Child Two")
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
VStack {
// Programmatic navigation here
NavigationLink(destination: ContentView(), isActive: $isShowingContentView) { EmptyView() }
Button("Add") {
self.isShowingContentView = true
}
}
}
}
}
}