SwiftUI - NavigationSplitView 内的 NavigationStack 内的嵌套链接不起作用

thi*_*ezn 9 swift swiftui swiftui-navigationsplitview macos-ventura

我正在尝试 ipadOS16/macOS13 中提供的新导航 API,但在弄清楚如何在 macOS 13 上将 NavigationSplitView、NavigationStack 和 NavigationLink 组合在一起时遇到了一些麻烦(在 Macbook Pro M1 上进行测试)。相同的代码在 ipadOS 上可以正常工作。

我正在使用两列 NavigationSplitView。在“详细信息”部分中,我有一个SampleModel1包含在 NavigationStack 中的实例列表。在列表中,我已为SampleModel1SampleModel2实例应用了 navigationDestination。

SampleModel1当我从列表中选择一个实例时,我会导航到一个详细视图,该视图本身包含一个SampleModel2实例列表。我的目的是在单击其中一个实例时进一步导航到 NavigationStack SampleModel2,但不幸的是这似乎不起作用。实例SampleModel2是可选择的,但没有发生导航。

当我完全删除 NavigationSplitView 并仅使用 NavigationStack 时,问题不会出现,并且我可以成功导航到SampleModel2实例。

这是我的示例代码:


import SwiftUI

@main
struct testingnavigationApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

// Sample model definitions used to trigger navigation with navigationDestination API.
struct SampleModel1: Hashable, Identifiable {
    let id = UUID()
    
    static let samples = [SampleModel1(), SampleModel1(), SampleModel1()]
}

struct SampleModel2: Hashable, Identifiable {
    let id = UUID()
    
    static let samples = [SampleModel2(), SampleModel2(), SampleModel2()]
}


// The initial view loaded by the app. This will initialize the NavigationSplitView
struct ContentView: View {
    
    enum NavItem {
        case first
    }
    
    var body: some View {
        NavigationSplitView {
            NavigationLink(value: NavItem.first) {
                Label("First", systemImage: "house")
            }
        } detail: {
            SampleListView()
        }
    }
}

// A list of SampleModel1 instances wrapped in a NavigationStack with multiple navigationDestinations
struct SampleListView: View {
    
    @State var path = NavigationPath()
    @State var selection: SampleModel1.ID? = nil
    
    var body: some View {
        NavigationStack(path: $path) {
            List(SampleModel1.samples, selection: $selection) { model in
                NavigationLink("\(model.id)", value: model)
            }
            .navigationDestination(for: SampleModel1.self) { model in
                SampleDetailView(model: model)
            }
            .navigationDestination(for: SampleModel2.self) { model in
                Text("Model 2 ID \(model.id)")
            }
        }
    }
}

// A detailed view of a single SampleModel1 instance. This includes a list
// of SampleModel2 instances that we would like to be able to navigate to
struct SampleDetailView: View {
    
    var model: SampleModel1
    
    var body: some View {
        Text("Model 1 ID \(model.id)")
        
        List (SampleModel2.samples) { model2 in
            NavigationLink("\(model2.id)", value: model2)
        }
    }
}




struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Run Code Online (Sandbox Code Playgroud)

thi*_*ezn 1

苹果今天回来提到这个问题应该在 macOS 13.3 Beta 中得到解决。

我已经测试过并可以确认该问题不再出现!macOS 和 iPad 操作系统上的导航现在可以正常工作了,耶!