tha*_*ris 4 swiftui swiftui-navigationlink ios16 swiftui-navigationsplitview ipados16
我正在尝试更新旧的应用程序以使用新的 NavigationSplitView 和 NavigationLink,但当侧边栏具有子对象层次结构并且我想从远处更新详细视图时,我试图以正确的方式进行操作子对象。例如,基于 WWDC2022 示例项目 Navigation Cookbook,我尝试了以下操作,但没有成功:
测试应用程序.swift
@main
struct TestApp: App {
@StateObject var appState = AppState()
var body: some Scene {
WindowGroup {
NavigationSplitView {
ProjectListView()
} detail: {
if let chapter = appState.chapter {
ChapterDetailView(chapter: chapter)
} else {
Text("Pick a Chapter")
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
ChapterListView.swift < ProjectListView() 的远亲(向下 3 级)兄弟
List(selection: $appState.chapter) {
ForEach(chapters) { chapter in
NavigationLink(chapter.title ?? "A Chapter", value: chapter)
}
}
Run Code Online (Sandbox Code Playgroud)
appState.swift
class AppState: ObservableObject {
@Published var chapter: Chapter?
}
Run Code Online (Sandbox Code Playgroud)
我确信我只是不了解新的导航方式如何工作的基础知识。是的,我的目标是 iOS16
这是 beta 1 中的一个已知错误。要解决此问题,需要将详细信息部分中的逻辑包装在 ZStack 中。从发行说明来看:
NavigationSplitView 列中的条件视图无法根据某些状态更改进行更新。(91311311) 解决方法:将列的内容包装在 ZStack 中。如果更改为这样,TestApp 可以工作:
@main
struct TestApp: App {
@StateObject var appState = AppState()
var body: some Scene {
WindowGroup {
NavigationSplitView {
ProjectListView()
} detail: {
// Wrap in a ZStack to fix this bug
ZStack {
if let chapter = appState.chapter {
ChapterDetailView(chapter: chapter)
} else {
Text("Pick a Chapter")
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
732 次 |
最近记录: |