SwiftUI 程序化导航问题

时间:2021-05-20 03:25:58

标签: ios swift xcode swiftui

我正在关注 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;手势识别器 = ;层 = ;内容偏移量:{0,-239};内容大小:{390, 169.33333333333334};调整内容插入:{239, 0, 34, 0};数据源:<< EM> TtGC7SwiftUIP13 $ 7fff56921d2819ListCoreCoordinatorGVS_20SystemListDataSourceOs5Never_GOS_19SelectionManagerBoxS2 的的:0x7fb75980e410 >>,新的观察员<< EM> TtGC7SwiftUI41StyleContextSplitViewNavigationControllerVS_19SidebarStyleContext 的:0x7fb758855000>,删除旧观察者<< EM> TtGC7SwiftUI41StyleContextSplitViewNavigationControllerVS_19SidebarStyleContext < /em>: 0x7fb75902ca00>

返回 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
                        }
                    }
                }
            }
    }
}

0 个答案:

没有答案